btrfs: introduce EXTENT_DIO_LOCKED

In order to support dropping the extent lock during a read we need a way
to make sure that direct reads and direct writes for overlapping ranges
are protected from each other.  To accomplish this introduce another
lock bit specifically for direct io.  Subsequent patches will utilize
this to protect direct IO operations.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Josef Bacik 2024-08-19 14:15:52 -04:00 committed by David Sterba
parent df2825e985
commit 7e2a595084
2 changed files with 58 additions and 35 deletions

View File

@ -126,7 +126,7 @@ void extent_io_tree_init(struct btrfs_fs_info *fs_info,
* Empty an io tree, removing and freeing every extent state record from the * Empty an io tree, removing and freeing every extent state record from the
* tree. This should be called once we are sure no other task can access the * tree. This should be called once we are sure no other task can access the
* tree anymore, so no tree updates happen after we empty the tree and there * tree anymore, so no tree updates happen after we empty the tree and there
* aren't any waiters on any extent state record (EXTENT_LOCKED bit is never * aren't any waiters on any extent state record (EXTENT_LOCK_BITS are never
* set on any extent state when calling this function). * set on any extent state when calling this function).
*/ */
void extent_io_tree_release(struct extent_io_tree *tree) void extent_io_tree_release(struct extent_io_tree *tree)
@ -141,7 +141,7 @@ void extent_io_tree_release(struct extent_io_tree *tree)
rbtree_postorder_for_each_entry_safe(state, tmp, &root, rb_node) { rbtree_postorder_for_each_entry_safe(state, tmp, &root, rb_node) {
/* Clear node to keep free_extent_state() happy. */ /* Clear node to keep free_extent_state() happy. */
RB_CLEAR_NODE(&state->rb_node); RB_CLEAR_NODE(&state->rb_node);
ASSERT(!(state->state & EXTENT_LOCKED)); ASSERT(!(state->state & EXTENT_LOCK_BITS));
/* /*
* No need for a memory barrier here, as we are holding the tree * No need for a memory barrier here, as we are holding the tree
* lock and we only change the waitqueue while holding that lock * lock and we only change the waitqueue while holding that lock
@ -399,7 +399,7 @@ static void merge_next_state(struct extent_io_tree *tree, struct extent_state *s
*/ */
static void merge_state(struct extent_io_tree *tree, struct extent_state *state) static void merge_state(struct extent_io_tree *tree, struct extent_state *state)
{ {
if (state->state & (EXTENT_LOCKED | EXTENT_BOUNDARY)) if (state->state & (EXTENT_LOCK_BITS | EXTENT_BOUNDARY))
return; return;
merge_prev_state(tree, state); merge_prev_state(tree, state);
@ -445,7 +445,7 @@ static struct extent_state *insert_state(struct extent_io_tree *tree,
struct rb_node *parent = NULL; struct rb_node *parent = NULL;
const u64 start = state->start - 1; const u64 start = state->start - 1;
const u64 end = state->end + 1; const u64 end = state->end + 1;
const bool try_merge = !(bits & (EXTENT_LOCKED | EXTENT_BOUNDARY)); const bool try_merge = !(bits & (EXTENT_LOCK_BITS | EXTENT_BOUNDARY));
set_state_bits(tree, state, bits, changeset); set_state_bits(tree, state, bits, changeset);
@ -616,9 +616,6 @@ static void set_gfp_mask_from_bits(u32 *bits, gfp_t *mask)
* inserting elements in the tree, so the gfp mask is used to indicate which * inserting elements in the tree, so the gfp mask is used to indicate which
* allocations or sleeping are allowed. * allocations or sleeping are allowed.
* *
* Pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove the given
* range from the tree regardless of state (ie for truncate).
*
* The range [start, end] is inclusive. * The range [start, end] is inclusive.
* *
* This takes the tree lock, and returns 0 on success and < 0 on error. * This takes the tree lock, and returns 0 on success and < 0 on error.
@ -647,8 +644,8 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
if (bits & EXTENT_DELALLOC) if (bits & EXTENT_DELALLOC)
bits |= EXTENT_NORESERVE; bits |= EXTENT_NORESERVE;
wake = (bits & EXTENT_LOCKED) ? 1 : 0; wake = ((bits & EXTENT_LOCK_BITS) ? 1 : 0);
if (bits & (EXTENT_LOCKED | EXTENT_BOUNDARY)) if (bits & (EXTENT_LOCK_BITS | EXTENT_BOUNDARY))
clear = 1; clear = 1;
again: again:
if (!prealloc) { if (!prealloc) {
@ -861,8 +858,7 @@ static void cache_state_if_flags(struct extent_state *state,
static void cache_state(struct extent_state *state, static void cache_state(struct extent_state *state,
struct extent_state **cached_ptr) struct extent_state **cached_ptr)
{ {
return cache_state_if_flags(state, cached_ptr, return cache_state_if_flags(state, cached_ptr, EXTENT_LOCK_BITS | EXTENT_BOUNDARY);
EXTENT_LOCKED | EXTENT_BOUNDARY);
} }
/* /*
@ -1063,7 +1059,7 @@ static int __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
int ret = 0; int ret = 0;
u64 last_start; u64 last_start;
u64 last_end; u64 last_end;
u32 exclusive_bits = (bits & EXTENT_LOCKED); u32 exclusive_bits = (bits & EXTENT_LOCK_BITS);
gfp_t mask; gfp_t mask;
set_gfp_mask_from_bits(&bits, &mask); set_gfp_mask_from_bits(&bits, &mask);
@ -1812,12 +1808,11 @@ int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
u32 bits, struct extent_changeset *changeset) u32 bits, struct extent_changeset *changeset)
{ {
/* /*
* We don't support EXTENT_LOCKED yet, as current changeset will * We don't support EXTENT_LOCK_BITS yet, as current changeset will
* record any bits changed, so for EXTENT_LOCKED case, it will * record any bits changed, so for EXTENT_LOCK_BITS case, it will either
* either fail with -EEXIST or changeset will record the whole * fail with -EEXIST or changeset will record the whole range.
* range.
*/ */
ASSERT(!(bits & EXTENT_LOCKED)); ASSERT(!(bits & EXTENT_LOCK_BITS));
return __set_extent_bit(tree, start, end, bits, NULL, NULL, NULL, changeset); return __set_extent_bit(tree, start, end, bits, NULL, NULL, NULL, changeset);
} }
@ -1826,26 +1821,25 @@ int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
u32 bits, struct extent_changeset *changeset) u32 bits, struct extent_changeset *changeset)
{ {
/* /*
* Don't support EXTENT_LOCKED case, same reason as * Don't support EXTENT_LOCK_BITS case, same reason as
* set_record_extent_bits(). * set_record_extent_bits().
*/ */
ASSERT(!(bits & EXTENT_LOCKED)); ASSERT(!(bits & EXTENT_LOCK_BITS));
return __clear_extent_bit(tree, start, end, bits, NULL, changeset); return __clear_extent_bit(tree, start, end, bits, NULL, changeset);
} }
int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, bool __try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
struct extent_state **cached) struct extent_state **cached)
{ {
int err; int err;
u64 failed_start; u64 failed_start;
err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, &failed_start, err = __set_extent_bit(tree, start, end, bits, &failed_start,
NULL, cached, NULL); NULL, cached, NULL);
if (err == -EEXIST) { if (err == -EEXIST) {
if (failed_start > start) if (failed_start > start)
clear_extent_bit(tree, start, failed_start - 1, clear_extent_bit(tree, start, failed_start - 1, bits, cached);
EXTENT_LOCKED, cached);
return 0; return 0;
} }
return 1; return 1;
@ -1855,23 +1849,22 @@ int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
* Either insert or lock state struct between start and end use mask to tell * Either insert or lock state struct between start and end use mask to tell
* us if waiting is desired. * us if waiting is desired.
*/ */
int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, int __lock_extent(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
struct extent_state **cached_state) struct extent_state **cached_state)
{ {
struct extent_state *failed_state = NULL; struct extent_state *failed_state = NULL;
int err; int err;
u64 failed_start; u64 failed_start;
err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, &failed_start, err = __set_extent_bit(tree, start, end, bits, &failed_start,
&failed_state, cached_state, NULL); &failed_state, cached_state, NULL);
while (err == -EEXIST) { while (err == -EEXIST) {
if (failed_start != start) if (failed_start != start)
clear_extent_bit(tree, start, failed_start - 1, clear_extent_bit(tree, start, failed_start - 1,
EXTENT_LOCKED, cached_state); bits, cached_state);
wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED, wait_extent_bit(tree, failed_start, end, bits, &failed_state);
&failed_state); err = __set_extent_bit(tree, start, end, bits,
err = __set_extent_bit(tree, start, end, EXTENT_LOCKED,
&failed_start, &failed_state, &failed_start, &failed_state,
cached_state, NULL); cached_state, NULL);
} }

View File

@ -19,6 +19,7 @@ enum {
ENUM_BIT(EXTENT_DIRTY), ENUM_BIT(EXTENT_DIRTY),
ENUM_BIT(EXTENT_UPTODATE), ENUM_BIT(EXTENT_UPTODATE),
ENUM_BIT(EXTENT_LOCKED), ENUM_BIT(EXTENT_LOCKED),
ENUM_BIT(EXTENT_DIO_LOCKED),
ENUM_BIT(EXTENT_NEW), ENUM_BIT(EXTENT_NEW),
ENUM_BIT(EXTENT_DELALLOC), ENUM_BIT(EXTENT_DELALLOC),
ENUM_BIT(EXTENT_DEFRAG), ENUM_BIT(EXTENT_DEFRAG),
@ -67,6 +68,8 @@ enum {
EXTENT_ADD_INODE_BYTES | \ EXTENT_ADD_INODE_BYTES | \
EXTENT_CLEAR_ALL_BITS) EXTENT_CLEAR_ALL_BITS)
#define EXTENT_LOCK_BITS (EXTENT_LOCKED | EXTENT_DIO_LOCKED)
/* /*
* Redefined bits above which are used only in the device allocation tree, * Redefined bits above which are used only in the device allocation tree,
* shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
@ -134,12 +137,22 @@ const struct btrfs_fs_info *extent_io_tree_to_fs_info(const struct extent_io_tre
void extent_io_tree_init(struct btrfs_fs_info *fs_info, void extent_io_tree_init(struct btrfs_fs_info *fs_info,
struct extent_io_tree *tree, unsigned int owner); struct extent_io_tree *tree, unsigned int owner);
void extent_io_tree_release(struct extent_io_tree *tree); void extent_io_tree_release(struct extent_io_tree *tree);
int __lock_extent(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
struct extent_state **cached);
bool __try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
struct extent_state **cached);
int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, static inline int lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
struct extent_state **cached); struct extent_state **cached)
{
return __lock_extent(tree, start, end, EXTENT_LOCKED, cached);
}
int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, static inline bool try_lock_extent(struct extent_io_tree *tree, u64 start,
struct extent_state **cached); u64 end, struct extent_state **cached)
{
return __try_lock_extent(tree, start, end, EXTENT_LOCKED, cached);
}
int __init extent_state_init_cachep(void); int __init extent_state_init_cachep(void);
void __cold extent_state_free_cachep(void); void __cold extent_state_free_cachep(void);
@ -212,5 +225,22 @@ int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
u64 *end, u64 max_bytes, u64 *end, u64 max_bytes,
struct extent_state **cached_state); struct extent_state **cached_state);
static inline int lock_dio_extent(struct extent_io_tree *tree, u64 start,
u64 end, struct extent_state **cached)
{
return __lock_extent(tree, start, end, EXTENT_DIO_LOCKED, cached);
}
static inline bool try_lock_dio_extent(struct extent_io_tree *tree, u64 start,
u64 end, struct extent_state **cached)
{
return __try_lock_extent(tree, start, end, EXTENT_DIO_LOCKED, cached);
}
static inline int unlock_dio_extent(struct extent_io_tree *tree, u64 start,
u64 end, struct extent_state **cached)
{
return __clear_extent_bit(tree, start, end, EXTENT_DIO_LOCKED, cached, NULL);
}
#endif /* BTRFS_EXTENT_IO_TREE_H */ #endif /* BTRFS_EXTENT_IO_TREE_H */