Commit 8ee92268 authored by Gabriel Niebler's avatar Gabriel Niebler Committed by David Sterba

btrfs: turn fs_info member buffer_radix into XArray

… named 'extent_buffers'. Also adjust all usages of this object to use
the XArray API, which greatly simplifies the code as it takes care of
locking and is generally easier to use and understand, providing
notionally simpler array semantics.

Also perform some light refactoring.
Reviewed-by: default avatarNikolay Borisov <nborisov@suse.com>
Signed-off-by: default avatarGabriel Niebler <gniebler@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 40769420
...@@ -994,10 +994,10 @@ struct btrfs_fs_info { ...@@ -994,10 +994,10 @@ struct btrfs_fs_info {
struct btrfs_delayed_root *delayed_root; struct btrfs_delayed_root *delayed_root;
/* Extent buffer radix tree */ /* Extent buffer xarray */
spinlock_t buffer_lock; spinlock_t buffer_lock;
/* Entries are eb->start / sectorsize */ /* Entries are eb->start / sectorsize */
struct radix_tree_root buffer_radix; struct xarray extent_buffers;
/* next backup root to be overwritten */ /* next backup root to be overwritten */
int backup_root_index; int backup_root_index;
......
...@@ -486,7 +486,7 @@ static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info, ...@@ -486,7 +486,7 @@ static int csum_dirty_subpage_buffers(struct btrfs_fs_info *fs_info,
uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur, uptodate = btrfs_subpage_test_uptodate(fs_info, page, cur,
fs_info->nodesize); fs_info->nodesize);
/* A dirty eb shouldn't disappear from buffer_radix */ /* A dirty eb shouldn't disappear from extent_buffers */
if (WARN_ON(!eb)) if (WARN_ON(!eb))
return -EUCLEAN; return -EUCLEAN;
...@@ -3151,7 +3151,7 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info) ...@@ -3151,7 +3151,7 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
void btrfs_init_fs_info(struct btrfs_fs_info *fs_info) void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
{ {
INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC); xa_init_flags(&fs_info->extent_buffers, GFP_ATOMIC);
INIT_LIST_HEAD(&fs_info->trans_list); INIT_LIST_HEAD(&fs_info->trans_list);
INIT_LIST_HEAD(&fs_info->dead_roots); INIT_LIST_HEAD(&fs_info->dead_roots);
INIT_LIST_HEAD(&fs_info->delayed_iputs); INIT_LIST_HEAD(&fs_info->delayed_iputs);
......
...@@ -2965,7 +2965,7 @@ static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page) ...@@ -2965,7 +2965,7 @@ static void begin_page_read(struct btrfs_fs_info *fs_info, struct page *page)
} }
/* /*
* Find extent buffer for a givne bytenr. * Find extent buffer for a given bytenr.
* *
* This is for end_bio_extent_readpage(), thus we can't do any unsafe locking * This is for end_bio_extent_readpage(), thus we can't do any unsafe locking
* in endio context. * in endio context.
...@@ -2984,11 +2984,9 @@ static struct extent_buffer *find_extent_buffer_readpage( ...@@ -2984,11 +2984,9 @@ static struct extent_buffer *find_extent_buffer_readpage(
return (struct extent_buffer *)page->private; return (struct extent_buffer *)page->private;
} }
/* For subpage case, we need to lookup buffer radix tree */ /* For subpage case, we need to lookup extent buffer xarray */
rcu_read_lock(); eb = xa_load(&fs_info->extent_buffers,
eb = radix_tree_lookup(&fs_info->buffer_radix,
bytenr >> fs_info->sectorsize_bits); bytenr >> fs_info->sectorsize_bits);
rcu_read_unlock();
ASSERT(eb); ASSERT(eb);
return eb; return eb;
} }
...@@ -4447,7 +4445,7 @@ static struct extent_buffer *find_extent_buffer_nolock( ...@@ -4447,7 +4445,7 @@ static struct extent_buffer *find_extent_buffer_nolock(
struct extent_buffer *eb; struct extent_buffer *eb;
rcu_read_lock(); rcu_read_lock();
eb = radix_tree_lookup(&fs_info->buffer_radix, eb = xa_load(&fs_info->extent_buffers,
start >> fs_info->sectorsize_bits); start >> fs_info->sectorsize_bits);
if (eb && atomic_inc_not_zero(&eb->refs)) { if (eb && atomic_inc_not_zero(&eb->refs)) {
rcu_read_unlock(); rcu_read_unlock();
...@@ -6141,24 +6139,22 @@ struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, ...@@ -6141,24 +6139,22 @@ struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
if (!eb) if (!eb)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
eb->fs_info = fs_info; eb->fs_info = fs_info;
again:
ret = radix_tree_preload(GFP_NOFS); do {
if (ret) { ret = xa_insert(&fs_info->extent_buffers,
start >> fs_info->sectorsize_bits,
eb, GFP_NOFS);
if (ret == -ENOMEM) {
exists = ERR_PTR(ret); exists = ERR_PTR(ret);
goto free_eb; goto free_eb;
} }
spin_lock(&fs_info->buffer_lock); if (ret == -EBUSY) {
ret = radix_tree_insert(&fs_info->buffer_radix,
start >> fs_info->sectorsize_bits, eb);
spin_unlock(&fs_info->buffer_lock);
radix_tree_preload_end();
if (ret == -EEXIST) {
exists = find_extent_buffer(fs_info, start); exists = find_extent_buffer(fs_info, start);
if (exists) if (exists)
goto free_eb; goto free_eb;
else
goto again;
} }
} while (ret);
check_buffer_tree_ref(eb); check_buffer_tree_ref(eb);
set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
...@@ -6333,25 +6329,22 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, ...@@ -6333,25 +6329,22 @@ struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
} }
if (uptodate) if (uptodate)
set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
again:
ret = radix_tree_preload(GFP_NOFS); do {
if (ret) { ret = xa_insert(&fs_info->extent_buffers,
start >> fs_info->sectorsize_bits,
eb, GFP_NOFS);
if (ret == -ENOMEM) {
exists = ERR_PTR(ret); exists = ERR_PTR(ret);
goto free_eb; goto free_eb;
} }
if (ret == -EBUSY) {
spin_lock(&fs_info->buffer_lock);
ret = radix_tree_insert(&fs_info->buffer_radix,
start >> fs_info->sectorsize_bits, eb);
spin_unlock(&fs_info->buffer_lock);
radix_tree_preload_end();
if (ret == -EEXIST) {
exists = find_extent_buffer(fs_info, start); exists = find_extent_buffer(fs_info, start);
if (exists) if (exists)
goto free_eb; goto free_eb;
else
goto again;
} }
} while (ret);
/* add one reference for the tree */ /* add one reference for the tree */
check_buffer_tree_ref(eb); check_buffer_tree_ref(eb);
set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags); set_bit(EXTENT_BUFFER_IN_TREE, &eb->bflags);
...@@ -6396,10 +6389,8 @@ static int release_extent_buffer(struct extent_buffer *eb) ...@@ -6396,10 +6389,8 @@ static int release_extent_buffer(struct extent_buffer *eb)
spin_unlock(&eb->refs_lock); spin_unlock(&eb->refs_lock);
spin_lock(&fs_info->buffer_lock); xa_erase(&fs_info->extent_buffers,
radix_tree_delete(&fs_info->buffer_radix,
eb->start >> fs_info->sectorsize_bits); eb->start >> fs_info->sectorsize_bits);
spin_unlock(&fs_info->buffer_lock);
} else { } else {
spin_unlock(&eb->refs_lock); spin_unlock(&eb->refs_lock);
} }
...@@ -7344,42 +7335,25 @@ void memmove_extent_buffer(const struct extent_buffer *dst, ...@@ -7344,42 +7335,25 @@ void memmove_extent_buffer(const struct extent_buffer *dst,
} }
} }
#define GANG_LOOKUP_SIZE 16
static struct extent_buffer *get_next_extent_buffer( static struct extent_buffer *get_next_extent_buffer(
struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr) struct btrfs_fs_info *fs_info, struct page *page, u64 bytenr)
{ {
struct extent_buffer *gang[GANG_LOOKUP_SIZE]; struct extent_buffer *eb;
struct extent_buffer *found = NULL; unsigned long index;
u64 page_start = page_offset(page); u64 page_start = page_offset(page);
u64 cur = page_start;
ASSERT(in_range(bytenr, page_start, PAGE_SIZE)); ASSERT(in_range(bytenr, page_start, PAGE_SIZE));
lockdep_assert_held(&fs_info->buffer_lock); lockdep_assert_held(&fs_info->buffer_lock);
while (cur < page_start + PAGE_SIZE) { xa_for_each_start(&fs_info->extent_buffers, index, eb,
int ret; page_start >> fs_info->sectorsize_bits) {
int i; if (in_range(eb->start, page_start, PAGE_SIZE))
return eb;
ret = radix_tree_gang_lookup(&fs_info->buffer_radix, else if (eb->start >= page_start + PAGE_SIZE)
(void **)gang, cur >> fs_info->sectorsize_bits,
min_t(unsigned int, GANG_LOOKUP_SIZE,
PAGE_SIZE / fs_info->nodesize));
if (ret == 0)
goto out;
for (i = 0; i < ret; i++) {
/* Already beyond page end */ /* Already beyond page end */
if (gang[i]->start >= page_start + PAGE_SIZE) return NULL;
goto out;
/* Found one */
if (gang[i]->start >= bytenr) {
found = gang[i];
goto out;
}
}
cur = gang[ret - 1]->start + gang[ret - 1]->len;
} }
out: return NULL;
return found;
} }
static int try_release_subpage_extent_buffer(struct page *page) static int try_release_subpage_extent_buffer(struct page *page)
......
...@@ -150,8 +150,8 @@ struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize) ...@@ -150,8 +150,8 @@ struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize)
void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info) void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
{ {
struct radix_tree_iter iter; unsigned long index;
void **slot; struct extent_buffer *eb;
struct btrfs_device *dev, *tmp; struct btrfs_device *dev, *tmp;
if (!fs_info) if (!fs_info)
...@@ -163,25 +163,9 @@ void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info) ...@@ -163,25 +163,9 @@ void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info)
test_mnt->mnt_sb->s_fs_info = NULL; test_mnt->mnt_sb->s_fs_info = NULL;
spin_lock(&fs_info->buffer_lock); xa_for_each(&fs_info->extent_buffers, index, eb) {
radix_tree_for_each_slot(slot, &fs_info->buffer_radix, &iter, 0) {
struct extent_buffer *eb;
eb = radix_tree_deref_slot_protected(slot, &fs_info->buffer_lock);
if (!eb)
continue;
/* Shouldn't happen but that kind of thinking creates CVE's */
if (radix_tree_exception(eb)) {
if (radix_tree_deref_retry(eb))
slot = radix_tree_iter_retry(&iter);
continue;
}
slot = radix_tree_iter_resume(slot, &iter);
spin_unlock(&fs_info->buffer_lock);
free_extent_buffer_stale(eb); free_extent_buffer_stale(eb);
spin_lock(&fs_info->buffer_lock);
} }
spin_unlock(&fs_info->buffer_lock);
btrfs_mapping_tree_free(&fs_info->mapping_tree); btrfs_mapping_tree_free(&fs_info->mapping_tree);
list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices, list_for_each_entry_safe(dev, tmp, &fs_info->fs_devices->devices,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment