Skip to content

Commit 662c653

Browse files
liubogithubkdave
authored andcommitted
Btrfs: grab write lock directly if write_lock_level is the max level
Typically, when acquiring root node's lock, btrfs tries its best to get read lock and trade for write lock if @write_lock_level implies to do so. In case of (cow && (p->keep_locks || p->lowest_level)), write_lock_level is set to BTRFS_MAX_LEVEL, which means we need to acquire root node's write lock directly. In this particular case, the dance of acquiring read lock and then trading for write lock can be saved. Signed-off-by: Liu Bo <bo.liu@linux.alibaba.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 1fc28d8 commit 662c653

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

fs/btrfs/ctree.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,19 +2632,24 @@ static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
26322632
}
26332633

26342634
/*
2635-
* We don't know the level of the root node until we actually have it
2636-
* read locked
2635+
* If the level is set to maximum, we can skip trying to get the read
2636+
* lock.
26372637
*/
2638-
b = btrfs_read_lock_root_node(root);
2639-
level = btrfs_header_level(b);
2640-
if (level > write_lock_level)
2641-
goto out;
2638+
if (write_lock_level < BTRFS_MAX_LEVEL) {
2639+
/*
2640+
* We don't know the level of the root node until we actually
2641+
* have it read locked
2642+
*/
2643+
b = btrfs_read_lock_root_node(root);
2644+
level = btrfs_header_level(b);
2645+
if (level > write_lock_level)
2646+
goto out;
2647+
2648+
/* Whoops, must trade for write lock */
2649+
btrfs_tree_read_unlock(b);
2650+
free_extent_buffer(b);
2651+
}
26422652

2643-
/*
2644-
* whoops, must trade for write lock
2645-
*/
2646-
btrfs_tree_read_unlock(b);
2647-
free_extent_buffer(b);
26482653
b = btrfs_lock_root_node(root);
26492654
root_lock = BTRFS_WRITE_LOCK;
26502655

0 commit comments

Comments
 (0)