mirror of
https://github.com/edk2-porting/linux-next.git
synced 2024-12-23 04:34:11 +08:00
f9690f426b
Commitdbcc7d57bf
("btrfs: fix race when cloning extent buffer during rewind of an old root"), fixed a race when we need to rewind the extent buffer of an old root. It was caused by picking a new mod log operation for the extent buffer while getting a cloned extent buffer with an outdated number of items (off by -1), because we cloned the extent buffer without locking it first. However there is still another similar race, but in the opposite direction. The cloned extent buffer has a number of items that does not match the number of tree mod log operations that are going to be replayed. This is because right after we got the last (most recent) tree mod log operation to replay and before locking and cloning the extent buffer, another task adds a new pointer to the extent buffer, which results in adding a new tree mod log operation and incrementing the number of items in the extent buffer. So after cloning we have mismatch between the number of items in the extent buffer and the number of mod log operations we are going to apply to it. This results in hitting a BUG_ON() that produces the following stack trace: ------------[ cut here ]------------ kernel BUG at fs/btrfs/tree-mod-log.c:675! invalid opcode: 0000 [#1] SMP KASAN PTI CPU: 3 PID: 4811 Comm: crawl_1215 Tainted: G W 5.12.0-7d1efdf501f8-misc-next+ #99 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014 RIP: 0010:tree_mod_log_rewind+0x3b1/0x3c0 Code: 05 48 8d 74 10 (...) RSP: 0018:ffffc90001027090 EFLAGS: 00010293 RAX: 0000000000000000 RBX: ffff8880a8514600 RCX: ffffffffaa9e59b6 RDX: 0000000000000007 RSI: dffffc0000000000 RDI: ffff8880a851462c RBP: ffffc900010270e0 R08: 00000000000000c0 R09: ffffed1004333417 R10: ffff88802199a0b7 R11: ffffed1004333416 R12: 000000000000000e R13: ffff888135af8748 R14: ffff88818766ff00 R15: ffff8880a851462c FS: 00007f29acf62700(0000) GS:ffff8881f2200000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f0e6013f718 CR3: 000000010d42e003 CR4: 0000000000170ee0 Call Trace: btrfs_get_old_root+0x16a/0x5c0 ? lock_downgrade+0x400/0x400 btrfs_search_old_slot+0x192/0x520 ? btrfs_search_slot+0x1090/0x1090 ? free_extent_buffer.part.61+0xd7/0x140 ? free_extent_buffer+0x13/0x20 resolve_indirect_refs+0x3e9/0xfc0 ? lock_downgrade+0x400/0x400 ? __kasan_check_read+0x11/0x20 ? add_prelim_ref.part.11+0x150/0x150 ? lock_downgrade+0x400/0x400 ? __kasan_check_read+0x11/0x20 ? lock_acquired+0xbb/0x620 ? __kasan_check_write+0x14/0x20 ? do_raw_spin_unlock+0xa8/0x140 ? rb_insert_color+0x340/0x360 ? prelim_ref_insert+0x12d/0x430 find_parent_nodes+0x5c3/0x1830 ? stack_trace_save+0x87/0xb0 ? resolve_indirect_refs+0xfc0/0xfc0 ? fs_reclaim_acquire+0x67/0xf0 ? __kasan_check_read+0x11/0x20 ? lockdep_hardirqs_on_prepare+0x210/0x210 ? fs_reclaim_acquire+0x67/0xf0 ? __kasan_check_read+0x11/0x20 ? ___might_sleep+0x10f/0x1e0 ? __kasan_kmalloc+0x9d/0xd0 ? trace_hardirqs_on+0x55/0x120 btrfs_find_all_roots_safe+0x142/0x1e0 ? find_parent_nodes+0x1830/0x1830 ? trace_hardirqs_on+0x55/0x120 ? ulist_free+0x1f/0x30 ? btrfs_inode_flags_to_xflags+0x50/0x50 iterate_extent_inodes+0x20e/0x580 ? tree_backref_for_extent+0x230/0x230 ? release_extent_buffer+0x225/0x280 ? read_extent_buffer+0xdd/0x110 ? lock_downgrade+0x400/0x400 ? __kasan_check_read+0x11/0x20 ? lock_acquired+0xbb/0x620 ? __kasan_check_write+0x14/0x20 ? do_raw_spin_unlock+0xa8/0x140 ? _raw_spin_unlock+0x22/0x30 ? release_extent_buffer+0x225/0x280 iterate_inodes_from_logical+0x129/0x170 ? iterate_inodes_from_logical+0x129/0x170 ? btrfs_inode_flags_to_xflags+0x50/0x50 ? iterate_extent_inodes+0x580/0x580 ? __vmalloc_node+0x92/0xb0 ? init_data_container+0x34/0xb0 ? init_data_container+0x34/0xb0 ? kvmalloc_node+0x60/0x80 btrfs_ioctl_logical_to_ino+0x158/0x230 btrfs_ioctl+0x2038/0x4360 ? __kasan_check_write+0x14/0x20 ? mmput+0x3b/0x220 ? btrfs_ioctl_get_supported_features+0x30/0x30 ? __kasan_check_read+0x11/0x20 ? __kasan_check_read+0x11/0x20 ? lock_release+0xc8/0x650 ? __might_fault+0x64/0xd0 ? __kasan_check_read+0x11/0x20 ? lock_downgrade+0x400/0x400 ? lockdep_hardirqs_on_prepare+0x210/0x210 ? lockdep_hardirqs_on_prepare+0x13/0x210 ? _raw_spin_unlock_irqrestore+0x51/0x63 ? __kasan_check_read+0x11/0x20 ? do_vfs_ioctl+0xfc/0x9d0 ? ioctl_file_clone+0xe0/0xe0 ? lock_downgrade+0x400/0x400 ? lockdep_hardirqs_on_prepare+0x210/0x210 ? __kasan_check_read+0x11/0x20 ? lock_release+0xc8/0x650 ? __task_pid_nr_ns+0xd3/0x250 ? __kasan_check_read+0x11/0x20 ? __fget_files+0x160/0x230 ? __fget_light+0xf2/0x110 __x64_sys_ioctl+0xc3/0x100 do_syscall_64+0x37/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xae RIP: 0033:0x7f29ae85b427 Code: 00 00 90 48 8b (...) RSP: 002b:00007f29acf5fcf8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010 RAX: ffffffffffffffda RBX: 00007f29acf5ff40 RCX: 00007f29ae85b427 RDX: 00007f29acf5ff48 RSI: 00000000c038943b RDI: 0000000000000003 RBP: 0000000001000000 R08: 0000000000000000 R09: 00007f29acf60120 R10: 00005640d5fc7b00 R11: 0000000000000246 R12: 0000000000000003 R13: 00007f29acf5ff48 R14: 00007f29acf5ff40 R15: 00007f29acf5fef8 Modules linked in: ---[ end trace 85e5fce078dfbe04 ]--- (gdb) l *(tree_mod_log_rewind+0x3b1) 0xffffffff819e5b21 is in tree_mod_log_rewind (fs/btrfs/tree-mod-log.c:675). 670 * the modification. As we're going backwards, we do the 671 * opposite of each operation here. 672 */ 673 switch (tm->op) { 674 case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING: 675 BUG_ON(tm->slot < n); 676 fallthrough; 677 case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING: 678 case BTRFS_MOD_LOG_KEY_REMOVE: 679 btrfs_set_node_key(eb, &tm->key, tm->slot); (gdb) quit The following steps explain in more detail how it happens: 1) We have one tree mod log user (through fiemap or the logical ino ioctl), with a sequence number of 1, so we have fs_info->tree_mod_seq == 1. This is task A; 2) Another task is at ctree.c:balance_level() and we have eb X currently as the root of the tree, and we promote its single child, eb Y, as the new root. Then, at ctree.c:balance_level(), we call: ret = btrfs_tree_mod_log_insert_root(root->node, child, true); 3) At btrfs_tree_mod_log_insert_root() we create a tree mod log operation of type BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING, with a ->logical field pointing to ebX->start. We only have one item in eb X, so we create only one tree mod log operation, and store in the "tm_list" array; 4) Then, still at btrfs_tree_mod_log_insert_root(), we create a tree mod log element of operation type BTRFS_MOD_LOG_ROOT_REPLACE, ->logical set to ebY->start, ->old_root.logical set to ebX->start, ->old_root.level set to the level of eb X and ->generation set to the generation of eb X; 5) Then btrfs_tree_mod_log_insert_root() calls tree_mod_log_free_eb() with "tm_list" as argument. After that, tree_mod_log_free_eb() calls tree_mod_log_insert(). This inserts the mod log operation of type BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING from step 3 into the rbtree with a sequence number of 2 (and fs_info->tree_mod_seq set to 2); 6) Then, after inserting the "tm_list" single element into the tree mod log rbtree, the BTRFS_MOD_LOG_ROOT_REPLACE element is inserted, which gets the sequence number 3 (and fs_info->tree_mod_seq set to 3); 7) Back to ctree.c:balance_level(), we free eb X by calling btrfs_free_tree_block() on it. Because eb X was created in the current transaction, has no other references and writeback did not happen for it, we add it back to the free space cache/tree; 8) Later some other task B allocates the metadata extent from eb X, since it is marked as free space in the space cache/tree, and uses it as a node for some other btree; 9) The tree mod log user task calls btrfs_search_old_slot(), which calls btrfs_get_old_root(), and finally that calls tree_mod_log_oldest_root() with time_seq == 1 and eb_root == eb Y; 10) The first iteration of the while loop finds the tree mod log element with sequence number 3, for the logical address of eb Y and of type BTRFS_MOD_LOG_ROOT_REPLACE; 11) Because the operation type is BTRFS_MOD_LOG_ROOT_REPLACE, we don't break out of the loop, and set root_logical to point to tm->old_root.logical, which corresponds to the logical address of eb X; 12) On the next iteration of the while loop, the call to tree_mod_log_search_oldest() returns the smallest tree mod log element for the logical address of eb X, which has a sequence number of 2, an operation type of BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING and corresponds to the old slot 0 of eb X (eb X had only 1 item in it before being freed at step 7); 13) We then break out of the while loop and return the tree mod log operation of type BTRFS_MOD_LOG_ROOT_REPLACE (eb Y), and not the one for slot 0 of eb X, to btrfs_get_old_root(); 14) At btrfs_get_old_root(), we process the BTRFS_MOD_LOG_ROOT_REPLACE operation and set "logical" to the logical address of eb X, which was the old root. We then call tree_mod_log_search() passing it the logical address of eb X and time_seq == 1; 15) But before calling tree_mod_log_search(), task B locks eb X, adds a key to eb X, which results in adding a tree mod log operation of type BTRFS_MOD_LOG_KEY_ADD, with a sequence number of 4, to the tree mod log, and increments the number of items in eb X from 0 to 1. Now fs_info->tree_mod_seq has a value of 4; 16) Task A then calls tree_mod_log_search(), which returns the most recent tree mod log operation for eb X, which is the one just added by task B at the previous step, with a sequence number of 4, a type of BTRFS_MOD_LOG_KEY_ADD and for slot 0; 17) Before task A locks and clones eb X, task A adds another key to eb X, which results in adding a new BTRFS_MOD_LOG_KEY_ADD mod log operation, with a sequence number of 5, for slot 1 of eb X, increments the number of items in eb X from 1 to 2, and unlocks eb X. Now fs_info->tree_mod_seq has a value of 5; 18) Task A then locks eb X and clones it. The clone has a value of 2 for the number of items and the pointer "tm" points to the tree mod log operation with sequence number 4, not the most recent one with a sequence number of 5, so there is mismatch between the number of mod log operations that are going to be applied to the cloned version of eb X and the number of items in the clone; 19) Task A then calls tree_mod_log_rewind() with the clone of eb X, the tree mod log operation with sequence number 4 and a type of BTRFS_MOD_LOG_KEY_ADD, and time_seq == 1; 20) At tree_mod_log_rewind(), we set the local variable "n" with a value of 2, which is the number of items in the clone of eb X. Then in the first iteration of the while loop, we process the mod log operation with sequence number 4, which is targeted at slot 0 and has a type of BTRFS_MOD_LOG_KEY_ADD. This results in decrementing "n" from 2 to 1. Then we pick the next tree mod log operation for eb X, which is the tree mod log operation with a sequence number of 2, a type of BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING and for slot 0, it is the one added in step 5 to the tree mod log tree. We go back to the top of the loop to process this mod log operation, and because its slot is 0 and "n" has a value of 1, we hit the BUG_ON: (...) switch (tm->op) { case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING: BUG_ON(tm->slot < n); fallthrough; (...) Fix this by checking for a more recent tree mod log operation after locking and cloning the extent buffer of the old root node, and use it as the first operation to apply to the cloned extent buffer when rewinding it. Stable backport notes: due to moved code and renames, in =< 5.11 the change should be applied to ctree.c:get_old_root. Reported-by: Zygo Blaxell <ce3g8jdj@umail.furryterror.org> Link: https://lore.kernel.org/linux-btrfs/20210404040732.GZ32440@hungrycats.org/ Fixes:834328a849
("Btrfs: tree mod log's old roots could still be part of the tree") CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
930 lines
24 KiB
C
930 lines
24 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include "tree-mod-log.h"
|
|
#include "disk-io.h"
|
|
|
|
struct tree_mod_root {
|
|
u64 logical;
|
|
u8 level;
|
|
};
|
|
|
|
struct tree_mod_elem {
|
|
struct rb_node node;
|
|
u64 logical;
|
|
u64 seq;
|
|
enum btrfs_mod_log_op op;
|
|
|
|
/*
|
|
* This is used for BTRFS_MOD_LOG_KEY_* and BTRFS_MOD_LOG_MOVE_KEYS
|
|
* operations.
|
|
*/
|
|
int slot;
|
|
|
|
/* This is used for BTRFS_MOD_LOG_KEY* and BTRFS_MOD_LOG_ROOT_REPLACE. */
|
|
u64 generation;
|
|
|
|
/* Those are used for op == BTRFS_MOD_LOG_KEY_{REPLACE,REMOVE}. */
|
|
struct btrfs_disk_key key;
|
|
u64 blockptr;
|
|
|
|
/* This is used for op == BTRFS_MOD_LOG_MOVE_KEYS. */
|
|
struct {
|
|
int dst_slot;
|
|
int nr_items;
|
|
} move;
|
|
|
|
/* This is used for op == BTRFS_MOD_LOG_ROOT_REPLACE. */
|
|
struct tree_mod_root old_root;
|
|
};
|
|
|
|
/*
|
|
* Pull a new tree mod seq number for our operation.
|
|
*/
|
|
static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
|
|
{
|
|
return atomic64_inc_return(&fs_info->tree_mod_seq);
|
|
}
|
|
|
|
/*
|
|
* This adds a new blocker to the tree mod log's blocker list if the @elem
|
|
* passed does not already have a sequence number set. So when a caller expects
|
|
* to record tree modifications, it should ensure to set elem->seq to zero
|
|
* before calling btrfs_get_tree_mod_seq.
|
|
* Returns a fresh, unused tree log modification sequence number, even if no new
|
|
* blocker was added.
|
|
*/
|
|
u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
|
|
struct btrfs_seq_list *elem)
|
|
{
|
|
write_lock(&fs_info->tree_mod_log_lock);
|
|
if (!elem->seq) {
|
|
elem->seq = btrfs_inc_tree_mod_seq(fs_info);
|
|
list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
|
|
set_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags);
|
|
}
|
|
write_unlock(&fs_info->tree_mod_log_lock);
|
|
|
|
return elem->seq;
|
|
}
|
|
|
|
void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
|
|
struct btrfs_seq_list *elem)
|
|
{
|
|
struct rb_root *tm_root;
|
|
struct rb_node *node;
|
|
struct rb_node *next;
|
|
struct tree_mod_elem *tm;
|
|
u64 min_seq = BTRFS_SEQ_LAST;
|
|
u64 seq_putting = elem->seq;
|
|
|
|
if (!seq_putting)
|
|
return;
|
|
|
|
write_lock(&fs_info->tree_mod_log_lock);
|
|
list_del(&elem->list);
|
|
elem->seq = 0;
|
|
|
|
if (list_empty(&fs_info->tree_mod_seq_list)) {
|
|
clear_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags);
|
|
} else {
|
|
struct btrfs_seq_list *first;
|
|
|
|
first = list_first_entry(&fs_info->tree_mod_seq_list,
|
|
struct btrfs_seq_list, list);
|
|
if (seq_putting > first->seq) {
|
|
/*
|
|
* Blocker with lower sequence number exists, we cannot
|
|
* remove anything from the log.
|
|
*/
|
|
write_unlock(&fs_info->tree_mod_log_lock);
|
|
return;
|
|
}
|
|
min_seq = first->seq;
|
|
}
|
|
|
|
/*
|
|
* Anything that's lower than the lowest existing (read: blocked)
|
|
* sequence number can be removed from the tree.
|
|
*/
|
|
tm_root = &fs_info->tree_mod_log;
|
|
for (node = rb_first(tm_root); node; node = next) {
|
|
next = rb_next(node);
|
|
tm = rb_entry(node, struct tree_mod_elem, node);
|
|
if (tm->seq >= min_seq)
|
|
continue;
|
|
rb_erase(node, tm_root);
|
|
kfree(tm);
|
|
}
|
|
write_unlock(&fs_info->tree_mod_log_lock);
|
|
}
|
|
|
|
/*
|
|
* Key order of the log:
|
|
* node/leaf start address -> sequence
|
|
*
|
|
* The 'start address' is the logical address of the *new* root node for root
|
|
* replace operations, or the logical address of the affected block for all
|
|
* other operations.
|
|
*/
|
|
static noinline int tree_mod_log_insert(struct btrfs_fs_info *fs_info,
|
|
struct tree_mod_elem *tm)
|
|
{
|
|
struct rb_root *tm_root;
|
|
struct rb_node **new;
|
|
struct rb_node *parent = NULL;
|
|
struct tree_mod_elem *cur;
|
|
|
|
lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
|
|
|
|
tm->seq = btrfs_inc_tree_mod_seq(fs_info);
|
|
|
|
tm_root = &fs_info->tree_mod_log;
|
|
new = &tm_root->rb_node;
|
|
while (*new) {
|
|
cur = rb_entry(*new, struct tree_mod_elem, node);
|
|
parent = *new;
|
|
if (cur->logical < tm->logical)
|
|
new = &((*new)->rb_left);
|
|
else if (cur->logical > tm->logical)
|
|
new = &((*new)->rb_right);
|
|
else if (cur->seq < tm->seq)
|
|
new = &((*new)->rb_left);
|
|
else if (cur->seq > tm->seq)
|
|
new = &((*new)->rb_right);
|
|
else
|
|
return -EEXIST;
|
|
}
|
|
|
|
rb_link_node(&tm->node, parent, new);
|
|
rb_insert_color(&tm->node, tm_root);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Determines if logging can be omitted. Returns true if it can. Otherwise, it
|
|
* returns false with the tree_mod_log_lock acquired. The caller must hold
|
|
* this until all tree mod log insertions are recorded in the rb tree and then
|
|
* write unlock fs_info::tree_mod_log_lock.
|
|
*/
|
|
static inline bool tree_mod_dont_log(struct btrfs_fs_info *fs_info,
|
|
struct extent_buffer *eb)
|
|
{
|
|
if (!test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
|
|
return true;
|
|
if (eb && btrfs_header_level(eb) == 0)
|
|
return true;
|
|
|
|
write_lock(&fs_info->tree_mod_log_lock);
|
|
if (list_empty(&(fs_info)->tree_mod_seq_list)) {
|
|
write_unlock(&fs_info->tree_mod_log_lock);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
|
|
static inline bool tree_mod_need_log(const struct btrfs_fs_info *fs_info,
|
|
struct extent_buffer *eb)
|
|
{
|
|
if (!test_bit(BTRFS_FS_TREE_MOD_LOG_USERS, &fs_info->flags))
|
|
return false;
|
|
if (eb && btrfs_header_level(eb) == 0)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
static struct tree_mod_elem *alloc_tree_mod_elem(struct extent_buffer *eb,
|
|
int slot,
|
|
enum btrfs_mod_log_op op,
|
|
gfp_t flags)
|
|
{
|
|
struct tree_mod_elem *tm;
|
|
|
|
tm = kzalloc(sizeof(*tm), flags);
|
|
if (!tm)
|
|
return NULL;
|
|
|
|
tm->logical = eb->start;
|
|
if (op != BTRFS_MOD_LOG_KEY_ADD) {
|
|
btrfs_node_key(eb, &tm->key, slot);
|
|
tm->blockptr = btrfs_node_blockptr(eb, slot);
|
|
}
|
|
tm->op = op;
|
|
tm->slot = slot;
|
|
tm->generation = btrfs_node_ptr_generation(eb, slot);
|
|
RB_CLEAR_NODE(&tm->node);
|
|
|
|
return tm;
|
|
}
|
|
|
|
int btrfs_tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
|
|
enum btrfs_mod_log_op op, gfp_t flags)
|
|
{
|
|
struct tree_mod_elem *tm;
|
|
int ret;
|
|
|
|
if (!tree_mod_need_log(eb->fs_info, eb))
|
|
return 0;
|
|
|
|
tm = alloc_tree_mod_elem(eb, slot, op, flags);
|
|
if (!tm)
|
|
return -ENOMEM;
|
|
|
|
if (tree_mod_dont_log(eb->fs_info, eb)) {
|
|
kfree(tm);
|
|
return 0;
|
|
}
|
|
|
|
ret = tree_mod_log_insert(eb->fs_info, tm);
|
|
write_unlock(&eb->fs_info->tree_mod_log_lock);
|
|
if (ret)
|
|
kfree(tm);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int btrfs_tree_mod_log_insert_move(struct extent_buffer *eb,
|
|
int dst_slot, int src_slot,
|
|
int nr_items)
|
|
{
|
|
struct tree_mod_elem *tm = NULL;
|
|
struct tree_mod_elem **tm_list = NULL;
|
|
int ret = 0;
|
|
int i;
|
|
bool locked = false;
|
|
|
|
if (!tree_mod_need_log(eb->fs_info, eb))
|
|
return 0;
|
|
|
|
tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
|
|
if (!tm_list)
|
|
return -ENOMEM;
|
|
|
|
tm = kzalloc(sizeof(*tm), GFP_NOFS);
|
|
if (!tm) {
|
|
ret = -ENOMEM;
|
|
goto free_tms;
|
|
}
|
|
|
|
tm->logical = eb->start;
|
|
tm->slot = src_slot;
|
|
tm->move.dst_slot = dst_slot;
|
|
tm->move.nr_items = nr_items;
|
|
tm->op = BTRFS_MOD_LOG_MOVE_KEYS;
|
|
|
|
for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
|
|
tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
|
|
BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
|
|
if (!tm_list[i]) {
|
|
ret = -ENOMEM;
|
|
goto free_tms;
|
|
}
|
|
}
|
|
|
|
if (tree_mod_dont_log(eb->fs_info, eb))
|
|
goto free_tms;
|
|
locked = true;
|
|
|
|
/*
|
|
* When we override something during the move, we log these removals.
|
|
* This can only happen when we move towards the beginning of the
|
|
* buffer, i.e. dst_slot < src_slot.
|
|
*/
|
|
for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
|
|
ret = tree_mod_log_insert(eb->fs_info, tm_list[i]);
|
|
if (ret)
|
|
goto free_tms;
|
|
}
|
|
|
|
ret = tree_mod_log_insert(eb->fs_info, tm);
|
|
if (ret)
|
|
goto free_tms;
|
|
write_unlock(&eb->fs_info->tree_mod_log_lock);
|
|
kfree(tm_list);
|
|
|
|
return 0;
|
|
|
|
free_tms:
|
|
for (i = 0; i < nr_items; i++) {
|
|
if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
|
|
rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
|
|
kfree(tm_list[i]);
|
|
}
|
|
if (locked)
|
|
write_unlock(&eb->fs_info->tree_mod_log_lock);
|
|
kfree(tm_list);
|
|
kfree(tm);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static inline int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
|
|
struct tree_mod_elem **tm_list,
|
|
int nritems)
|
|
{
|
|
int i, j;
|
|
int ret;
|
|
|
|
for (i = nritems - 1; i >= 0; i--) {
|
|
ret = tree_mod_log_insert(fs_info, tm_list[i]);
|
|
if (ret) {
|
|
for (j = nritems - 1; j > i; j--)
|
|
rb_erase(&tm_list[j]->node,
|
|
&fs_info->tree_mod_log);
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int btrfs_tree_mod_log_insert_root(struct extent_buffer *old_root,
|
|
struct extent_buffer *new_root,
|
|
bool log_removal)
|
|
{
|
|
struct btrfs_fs_info *fs_info = old_root->fs_info;
|
|
struct tree_mod_elem *tm = NULL;
|
|
struct tree_mod_elem **tm_list = NULL;
|
|
int nritems = 0;
|
|
int ret = 0;
|
|
int i;
|
|
|
|
if (!tree_mod_need_log(fs_info, NULL))
|
|
return 0;
|
|
|
|
if (log_removal && btrfs_header_level(old_root) > 0) {
|
|
nritems = btrfs_header_nritems(old_root);
|
|
tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
|
|
GFP_NOFS);
|
|
if (!tm_list) {
|
|
ret = -ENOMEM;
|
|
goto free_tms;
|
|
}
|
|
for (i = 0; i < nritems; i++) {
|
|
tm_list[i] = alloc_tree_mod_elem(old_root, i,
|
|
BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
|
|
if (!tm_list[i]) {
|
|
ret = -ENOMEM;
|
|
goto free_tms;
|
|
}
|
|
}
|
|
}
|
|
|
|
tm = kzalloc(sizeof(*tm), GFP_NOFS);
|
|
if (!tm) {
|
|
ret = -ENOMEM;
|
|
goto free_tms;
|
|
}
|
|
|
|
tm->logical = new_root->start;
|
|
tm->old_root.logical = old_root->start;
|
|
tm->old_root.level = btrfs_header_level(old_root);
|
|
tm->generation = btrfs_header_generation(old_root);
|
|
tm->op = BTRFS_MOD_LOG_ROOT_REPLACE;
|
|
|
|
if (tree_mod_dont_log(fs_info, NULL))
|
|
goto free_tms;
|
|
|
|
if (tm_list)
|
|
ret = tree_mod_log_free_eb(fs_info, tm_list, nritems);
|
|
if (!ret)
|
|
ret = tree_mod_log_insert(fs_info, tm);
|
|
|
|
write_unlock(&fs_info->tree_mod_log_lock);
|
|
if (ret)
|
|
goto free_tms;
|
|
kfree(tm_list);
|
|
|
|
return ret;
|
|
|
|
free_tms:
|
|
if (tm_list) {
|
|
for (i = 0; i < nritems; i++)
|
|
kfree(tm_list[i]);
|
|
kfree(tm_list);
|
|
}
|
|
kfree(tm);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static struct tree_mod_elem *__tree_mod_log_search(struct btrfs_fs_info *fs_info,
|
|
u64 start, u64 min_seq,
|
|
bool smallest)
|
|
{
|
|
struct rb_root *tm_root;
|
|
struct rb_node *node;
|
|
struct tree_mod_elem *cur = NULL;
|
|
struct tree_mod_elem *found = NULL;
|
|
|
|
read_lock(&fs_info->tree_mod_log_lock);
|
|
tm_root = &fs_info->tree_mod_log;
|
|
node = tm_root->rb_node;
|
|
while (node) {
|
|
cur = rb_entry(node, struct tree_mod_elem, node);
|
|
if (cur->logical < start) {
|
|
node = node->rb_left;
|
|
} else if (cur->logical > start) {
|
|
node = node->rb_right;
|
|
} else if (cur->seq < min_seq) {
|
|
node = node->rb_left;
|
|
} else if (!smallest) {
|
|
/* We want the node with the highest seq */
|
|
if (found)
|
|
BUG_ON(found->seq > cur->seq);
|
|
found = cur;
|
|
node = node->rb_left;
|
|
} else if (cur->seq > min_seq) {
|
|
/* We want the node with the smallest seq */
|
|
if (found)
|
|
BUG_ON(found->seq < cur->seq);
|
|
found = cur;
|
|
node = node->rb_right;
|
|
} else {
|
|
found = cur;
|
|
break;
|
|
}
|
|
}
|
|
read_unlock(&fs_info->tree_mod_log_lock);
|
|
|
|
return found;
|
|
}
|
|
|
|
/*
|
|
* This returns the element from the log with the smallest time sequence
|
|
* value that's in the log (the oldest log item). Any element with a time
|
|
* sequence lower than min_seq will be ignored.
|
|
*/
|
|
static struct tree_mod_elem *tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info,
|
|
u64 start, u64 min_seq)
|
|
{
|
|
return __tree_mod_log_search(fs_info, start, min_seq, true);
|
|
}
|
|
|
|
/*
|
|
* This returns the element from the log with the largest time sequence
|
|
* value that's in the log (the most recent log item). Any element with
|
|
* a time sequence lower than min_seq will be ignored.
|
|
*/
|
|
static struct tree_mod_elem *tree_mod_log_search(struct btrfs_fs_info *fs_info,
|
|
u64 start, u64 min_seq)
|
|
{
|
|
return __tree_mod_log_search(fs_info, start, min_seq, false);
|
|
}
|
|
|
|
int btrfs_tree_mod_log_eb_copy(struct extent_buffer *dst,
|
|
struct extent_buffer *src,
|
|
unsigned long dst_offset,
|
|
unsigned long src_offset,
|
|
int nr_items)
|
|
{
|
|
struct btrfs_fs_info *fs_info = dst->fs_info;
|
|
int ret = 0;
|
|
struct tree_mod_elem **tm_list = NULL;
|
|
struct tree_mod_elem **tm_list_add, **tm_list_rem;
|
|
int i;
|
|
bool locked = false;
|
|
|
|
if (!tree_mod_need_log(fs_info, NULL))
|
|
return 0;
|
|
|
|
if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
|
|
return 0;
|
|
|
|
tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
|
|
GFP_NOFS);
|
|
if (!tm_list)
|
|
return -ENOMEM;
|
|
|
|
tm_list_add = tm_list;
|
|
tm_list_rem = tm_list + nr_items;
|
|
for (i = 0; i < nr_items; i++) {
|
|
tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
|
|
BTRFS_MOD_LOG_KEY_REMOVE, GFP_NOFS);
|
|
if (!tm_list_rem[i]) {
|
|
ret = -ENOMEM;
|
|
goto free_tms;
|
|
}
|
|
|
|
tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
|
|
BTRFS_MOD_LOG_KEY_ADD, GFP_NOFS);
|
|
if (!tm_list_add[i]) {
|
|
ret = -ENOMEM;
|
|
goto free_tms;
|
|
}
|
|
}
|
|
|
|
if (tree_mod_dont_log(fs_info, NULL))
|
|
goto free_tms;
|
|
locked = true;
|
|
|
|
for (i = 0; i < nr_items; i++) {
|
|
ret = tree_mod_log_insert(fs_info, tm_list_rem[i]);
|
|
if (ret)
|
|
goto free_tms;
|
|
ret = tree_mod_log_insert(fs_info, tm_list_add[i]);
|
|
if (ret)
|
|
goto free_tms;
|
|
}
|
|
|
|
write_unlock(&fs_info->tree_mod_log_lock);
|
|
kfree(tm_list);
|
|
|
|
return 0;
|
|
|
|
free_tms:
|
|
for (i = 0; i < nr_items * 2; i++) {
|
|
if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
|
|
rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
|
|
kfree(tm_list[i]);
|
|
}
|
|
if (locked)
|
|
write_unlock(&fs_info->tree_mod_log_lock);
|
|
kfree(tm_list);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int btrfs_tree_mod_log_free_eb(struct extent_buffer *eb)
|
|
{
|
|
struct tree_mod_elem **tm_list = NULL;
|
|
int nritems = 0;
|
|
int i;
|
|
int ret = 0;
|
|
|
|
if (!tree_mod_need_log(eb->fs_info, eb))
|
|
return 0;
|
|
|
|
nritems = btrfs_header_nritems(eb);
|
|
tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
|
|
if (!tm_list)
|
|
return -ENOMEM;
|
|
|
|
for (i = 0; i < nritems; i++) {
|
|
tm_list[i] = alloc_tree_mod_elem(eb, i,
|
|
BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
|
|
if (!tm_list[i]) {
|
|
ret = -ENOMEM;
|
|
goto free_tms;
|
|
}
|
|
}
|
|
|
|
if (tree_mod_dont_log(eb->fs_info, eb))
|
|
goto free_tms;
|
|
|
|
ret = tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
|
|
write_unlock(&eb->fs_info->tree_mod_log_lock);
|
|
if (ret)
|
|
goto free_tms;
|
|
kfree(tm_list);
|
|
|
|
return 0;
|
|
|
|
free_tms:
|
|
for (i = 0; i < nritems; i++)
|
|
kfree(tm_list[i]);
|
|
kfree(tm_list);
|
|
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Returns the logical address of the oldest predecessor of the given root.
|
|
* Entries older than time_seq are ignored.
|
|
*/
|
|
static struct tree_mod_elem *tree_mod_log_oldest_root(struct extent_buffer *eb_root,
|
|
u64 time_seq)
|
|
{
|
|
struct tree_mod_elem *tm;
|
|
struct tree_mod_elem *found = NULL;
|
|
u64 root_logical = eb_root->start;
|
|
bool looped = false;
|
|
|
|
if (!time_seq)
|
|
return NULL;
|
|
|
|
/*
|
|
* The very last operation that's logged for a root is the replacement
|
|
* operation (if it is replaced at all). This has the logical address
|
|
* of the *new* root, making it the very first operation that's logged
|
|
* for this root.
|
|
*/
|
|
while (1) {
|
|
tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
|
|
time_seq);
|
|
if (!looped && !tm)
|
|
return NULL;
|
|
/*
|
|
* If there are no tree operation for the oldest root, we simply
|
|
* return it. This should only happen if that (old) root is at
|
|
* level 0.
|
|
*/
|
|
if (!tm)
|
|
break;
|
|
|
|
/*
|
|
* If there's an operation that's not a root replacement, we
|
|
* found the oldest version of our root. Normally, we'll find a
|
|
* BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
|
|
*/
|
|
if (tm->op != BTRFS_MOD_LOG_ROOT_REPLACE)
|
|
break;
|
|
|
|
found = tm;
|
|
root_logical = tm->old_root.logical;
|
|
looped = true;
|
|
}
|
|
|
|
/* If there's no old root to return, return what we found instead */
|
|
if (!found)
|
|
found = tm;
|
|
|
|
return found;
|
|
}
|
|
|
|
|
|
/*
|
|
* tm is a pointer to the first operation to rewind within eb. Then, all
|
|
* previous operations will be rewound (until we reach something older than
|
|
* time_seq).
|
|
*/
|
|
static void tree_mod_log_rewind(struct btrfs_fs_info *fs_info,
|
|
struct extent_buffer *eb,
|
|
u64 time_seq,
|
|
struct tree_mod_elem *first_tm)
|
|
{
|
|
u32 n;
|
|
struct rb_node *next;
|
|
struct tree_mod_elem *tm = first_tm;
|
|
unsigned long o_dst;
|
|
unsigned long o_src;
|
|
unsigned long p_size = sizeof(struct btrfs_key_ptr);
|
|
|
|
n = btrfs_header_nritems(eb);
|
|
read_lock(&fs_info->tree_mod_log_lock);
|
|
while (tm && tm->seq >= time_seq) {
|
|
/*
|
|
* All the operations are recorded with the operator used for
|
|
* the modification. As we're going backwards, we do the
|
|
* opposite of each operation here.
|
|
*/
|
|
switch (tm->op) {
|
|
case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING:
|
|
BUG_ON(tm->slot < n);
|
|
fallthrough;
|
|
case BTRFS_MOD_LOG_KEY_REMOVE_WHILE_MOVING:
|
|
case BTRFS_MOD_LOG_KEY_REMOVE:
|
|
btrfs_set_node_key(eb, &tm->key, tm->slot);
|
|
btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
|
|
btrfs_set_node_ptr_generation(eb, tm->slot,
|
|
tm->generation);
|
|
n++;
|
|
break;
|
|
case BTRFS_MOD_LOG_KEY_REPLACE:
|
|
BUG_ON(tm->slot >= n);
|
|
btrfs_set_node_key(eb, &tm->key, tm->slot);
|
|
btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
|
|
btrfs_set_node_ptr_generation(eb, tm->slot,
|
|
tm->generation);
|
|
break;
|
|
case BTRFS_MOD_LOG_KEY_ADD:
|
|
/* if a move operation is needed it's in the log */
|
|
n--;
|
|
break;
|
|
case BTRFS_MOD_LOG_MOVE_KEYS:
|
|
o_dst = btrfs_node_key_ptr_offset(tm->slot);
|
|
o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
|
|
memmove_extent_buffer(eb, o_dst, o_src,
|
|
tm->move.nr_items * p_size);
|
|
break;
|
|
case BTRFS_MOD_LOG_ROOT_REPLACE:
|
|
/*
|
|
* This operation is special. For roots, this must be
|
|
* handled explicitly before rewinding.
|
|
* For non-roots, this operation may exist if the node
|
|
* was a root: root A -> child B; then A gets empty and
|
|
* B is promoted to the new root. In the mod log, we'll
|
|
* have a root-replace operation for B, a tree block
|
|
* that is no root. We simply ignore that operation.
|
|
*/
|
|
break;
|
|
}
|
|
next = rb_next(&tm->node);
|
|
if (!next)
|
|
break;
|
|
tm = rb_entry(next, struct tree_mod_elem, node);
|
|
if (tm->logical != first_tm->logical)
|
|
break;
|
|
}
|
|
read_unlock(&fs_info->tree_mod_log_lock);
|
|
btrfs_set_header_nritems(eb, n);
|
|
}
|
|
|
|
/*
|
|
* Called with eb read locked. If the buffer cannot be rewound, the same buffer
|
|
* is returned. If rewind operations happen, a fresh buffer is returned. The
|
|
* returned buffer is always read-locked. If the returned buffer is not the
|
|
* input buffer, the lock on the input buffer is released and the input buffer
|
|
* is freed (its refcount is decremented).
|
|
*/
|
|
struct extent_buffer *btrfs_tree_mod_log_rewind(struct btrfs_fs_info *fs_info,
|
|
struct btrfs_path *path,
|
|
struct extent_buffer *eb,
|
|
u64 time_seq)
|
|
{
|
|
struct extent_buffer *eb_rewin;
|
|
struct tree_mod_elem *tm;
|
|
|
|
if (!time_seq)
|
|
return eb;
|
|
|
|
if (btrfs_header_level(eb) == 0)
|
|
return eb;
|
|
|
|
tm = tree_mod_log_search(fs_info, eb->start, time_seq);
|
|
if (!tm)
|
|
return eb;
|
|
|
|
if (tm->op == BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
|
|
BUG_ON(tm->slot != 0);
|
|
eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
|
|
if (!eb_rewin) {
|
|
btrfs_tree_read_unlock(eb);
|
|
free_extent_buffer(eb);
|
|
return NULL;
|
|
}
|
|
btrfs_set_header_bytenr(eb_rewin, eb->start);
|
|
btrfs_set_header_backref_rev(eb_rewin,
|
|
btrfs_header_backref_rev(eb));
|
|
btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
|
|
btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
|
|
} else {
|
|
eb_rewin = btrfs_clone_extent_buffer(eb);
|
|
if (!eb_rewin) {
|
|
btrfs_tree_read_unlock(eb);
|
|
free_extent_buffer(eb);
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
btrfs_tree_read_unlock(eb);
|
|
free_extent_buffer(eb);
|
|
|
|
btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
|
|
eb_rewin, btrfs_header_level(eb_rewin));
|
|
btrfs_tree_read_lock(eb_rewin);
|
|
tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
|
|
WARN_ON(btrfs_header_nritems(eb_rewin) >
|
|
BTRFS_NODEPTRS_PER_BLOCK(fs_info));
|
|
|
|
return eb_rewin;
|
|
}
|
|
|
|
/*
|
|
* Rewind the state of @root's root node to the given @time_seq value.
|
|
* If there are no changes, the current root->root_node is returned. If anything
|
|
* changed in between, there's a fresh buffer allocated on which the rewind
|
|
* operations are done. In any case, the returned buffer is read locked.
|
|
* Returns NULL on error (with no locks held).
|
|
*/
|
|
struct extent_buffer *btrfs_get_old_root(struct btrfs_root *root, u64 time_seq)
|
|
{
|
|
struct btrfs_fs_info *fs_info = root->fs_info;
|
|
struct tree_mod_elem *tm;
|
|
struct extent_buffer *eb = NULL;
|
|
struct extent_buffer *eb_root;
|
|
u64 eb_root_owner = 0;
|
|
struct extent_buffer *old;
|
|
struct tree_mod_root *old_root = NULL;
|
|
u64 old_generation = 0;
|
|
u64 logical;
|
|
int level;
|
|
|
|
eb_root = btrfs_read_lock_root_node(root);
|
|
tm = tree_mod_log_oldest_root(eb_root, time_seq);
|
|
if (!tm)
|
|
return eb_root;
|
|
|
|
if (tm->op == BTRFS_MOD_LOG_ROOT_REPLACE) {
|
|
old_root = &tm->old_root;
|
|
old_generation = tm->generation;
|
|
logical = old_root->logical;
|
|
level = old_root->level;
|
|
} else {
|
|
logical = eb_root->start;
|
|
level = btrfs_header_level(eb_root);
|
|
}
|
|
|
|
tm = tree_mod_log_search(fs_info, logical, time_seq);
|
|
if (old_root && tm && tm->op != BTRFS_MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
|
|
btrfs_tree_read_unlock(eb_root);
|
|
free_extent_buffer(eb_root);
|
|
old = read_tree_block(fs_info, logical, root->root_key.objectid,
|
|
0, level, NULL);
|
|
if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
|
|
if (!IS_ERR(old))
|
|
free_extent_buffer(old);
|
|
btrfs_warn(fs_info,
|
|
"failed to read tree block %llu from get_old_root",
|
|
logical);
|
|
} else {
|
|
struct tree_mod_elem *tm2;
|
|
|
|
btrfs_tree_read_lock(old);
|
|
eb = btrfs_clone_extent_buffer(old);
|
|
/*
|
|
* After the lookup for the most recent tree mod operation
|
|
* above and before we locked and cloned the extent buffer
|
|
* 'old', a new tree mod log operation may have been added.
|
|
* So lookup for a more recent one to make sure the number
|
|
* of mod log operations we replay is consistent with the
|
|
* number of items we have in the cloned extent buffer,
|
|
* otherwise we can hit a BUG_ON when rewinding the extent
|
|
* buffer.
|
|
*/
|
|
tm2 = tree_mod_log_search(fs_info, logical, time_seq);
|
|
btrfs_tree_read_unlock(old);
|
|
free_extent_buffer(old);
|
|
ASSERT(tm2);
|
|
ASSERT(tm2 == tm || tm2->seq > tm->seq);
|
|
if (!tm2 || tm2->seq < tm->seq) {
|
|
free_extent_buffer(eb);
|
|
return NULL;
|
|
}
|
|
tm = tm2;
|
|
}
|
|
} else if (old_root) {
|
|
eb_root_owner = btrfs_header_owner(eb_root);
|
|
btrfs_tree_read_unlock(eb_root);
|
|
free_extent_buffer(eb_root);
|
|
eb = alloc_dummy_extent_buffer(fs_info, logical);
|
|
} else {
|
|
eb = btrfs_clone_extent_buffer(eb_root);
|
|
btrfs_tree_read_unlock(eb_root);
|
|
free_extent_buffer(eb_root);
|
|
}
|
|
|
|
if (!eb)
|
|
return NULL;
|
|
if (old_root) {
|
|
btrfs_set_header_bytenr(eb, eb->start);
|
|
btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
|
|
btrfs_set_header_owner(eb, eb_root_owner);
|
|
btrfs_set_header_level(eb, old_root->level);
|
|
btrfs_set_header_generation(eb, old_generation);
|
|
}
|
|
btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
|
|
btrfs_header_level(eb));
|
|
btrfs_tree_read_lock(eb);
|
|
if (tm)
|
|
tree_mod_log_rewind(fs_info, eb, time_seq, tm);
|
|
else
|
|
WARN_ON(btrfs_header_level(eb) != 0);
|
|
WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
|
|
|
|
return eb;
|
|
}
|
|
|
|
int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
|
|
{
|
|
struct tree_mod_elem *tm;
|
|
int level;
|
|
struct extent_buffer *eb_root = btrfs_root_node(root);
|
|
|
|
tm = tree_mod_log_oldest_root(eb_root, time_seq);
|
|
if (tm && tm->op == BTRFS_MOD_LOG_ROOT_REPLACE)
|
|
level = tm->old_root.level;
|
|
else
|
|
level = btrfs_header_level(eb_root);
|
|
|
|
free_extent_buffer(eb_root);
|
|
|
|
return level;
|
|
}
|
|
|
|
/*
|
|
* Return the lowest sequence number in the tree modification log.
|
|
*
|
|
* Return the sequence number of the oldest tree modification log user, which
|
|
* corresponds to the lowest sequence number of all existing users. If there are
|
|
* no users it returns 0.
|
|
*/
|
|
u64 btrfs_tree_mod_log_lowest_seq(struct btrfs_fs_info *fs_info)
|
|
{
|
|
u64 ret = 0;
|
|
|
|
read_lock(&fs_info->tree_mod_log_lock);
|
|
if (!list_empty(&fs_info->tree_mod_seq_list)) {
|
|
struct btrfs_seq_list *elem;
|
|
|
|
elem = list_first_entry(&fs_info->tree_mod_seq_list,
|
|
struct btrfs_seq_list, list);
|
|
ret = elem->seq;
|
|
}
|
|
read_unlock(&fs_info->tree_mod_log_lock);
|
|
|
|
return ret;
|
|
}
|