Commit 9c5c9604 authored by Josef Bacik's avatar Josef Bacik Committed by David Sterba

btrfs: use a cached_state everywhere in relocation

All of the relocation code avoids using the cached state, despite
everywhere using the normal

  lock_extent()
  // do something
  unlock_extent()

pattern.  Fix this by plumbing a cached state throughout all of these
functions in order to allow for less tree searches.
Reviewed-by: default avatarFilipe Manana <fdmanana@suse.com>
Signed-off-by: default avatarJosef Bacik <josef@toxicpanda.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 632ddfa2
...@@ -1113,6 +1113,8 @@ int replace_file_extents(struct btrfs_trans_handle *trans, ...@@ -1113,6 +1113,8 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
inode = find_next_inode(root, key.objectid); inode = find_next_inode(root, key.objectid);
} }
if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) { if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) {
struct extent_state *cached_state = NULL;
end = key.offset + end = key.offset +
btrfs_file_extent_num_bytes(leaf, fi); btrfs_file_extent_num_bytes(leaf, fi);
WARN_ON(!IS_ALIGNED(key.offset, WARN_ON(!IS_ALIGNED(key.offset,
...@@ -1120,14 +1122,15 @@ int replace_file_extents(struct btrfs_trans_handle *trans, ...@@ -1120,14 +1122,15 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize)); WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
end--; end--;
ret = try_lock_extent(&BTRFS_I(inode)->io_tree, ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
key.offset, end, NULL); key.offset, end,
&cached_state);
if (!ret) if (!ret)
continue; continue;
btrfs_drop_extent_map_range(BTRFS_I(inode), btrfs_drop_extent_map_range(BTRFS_I(inode),
key.offset, end, true); key.offset, end, true);
unlock_extent(&BTRFS_I(inode)->io_tree, unlock_extent(&BTRFS_I(inode)->io_tree,
key.offset, end, NULL); key.offset, end, &cached_state);
} }
} }
...@@ -1516,6 +1519,8 @@ static int invalidate_extent_cache(struct btrfs_root *root, ...@@ -1516,6 +1519,8 @@ static int invalidate_extent_cache(struct btrfs_root *root,
objectid = min_key->objectid; objectid = min_key->objectid;
while (1) { while (1) {
struct extent_state *cached_state = NULL;
cond_resched(); cond_resched();
iput(inode); iput(inode);
...@@ -1566,9 +1571,9 @@ static int invalidate_extent_cache(struct btrfs_root *root, ...@@ -1566,9 +1571,9 @@ static int invalidate_extent_cache(struct btrfs_root *root,
} }
/* the lock_extent waits for read_folio to complete */ /* the lock_extent waits for read_folio to complete */
lock_extent(&BTRFS_I(inode)->io_tree, start, end, NULL); lock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);
btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, true); btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, true);
unlock_extent(&BTRFS_I(inode)->io_tree, start, end, NULL); unlock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);
} }
return 0; return 0;
} }
...@@ -2863,19 +2868,21 @@ static noinline_for_stack int prealloc_file_extent_cluster( ...@@ -2863,19 +2868,21 @@ static noinline_for_stack int prealloc_file_extent_cluster(
btrfs_inode_lock(&inode->vfs_inode, 0); btrfs_inode_lock(&inode->vfs_inode, 0);
for (nr = 0; nr < cluster->nr; nr++) { for (nr = 0; nr < cluster->nr; nr++) {
struct extent_state *cached_state = NULL;
start = cluster->boundary[nr] - offset; start = cluster->boundary[nr] - offset;
if (nr + 1 < cluster->nr) if (nr + 1 < cluster->nr)
end = cluster->boundary[nr + 1] - 1 - offset; end = cluster->boundary[nr + 1] - 1 - offset;
else else
end = cluster->end - offset; end = cluster->end - offset;
lock_extent(&inode->io_tree, start, end, NULL); lock_extent(&inode->io_tree, start, end, &cached_state);
num_bytes = end + 1 - start; num_bytes = end + 1 - start;
ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start, ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start,
num_bytes, num_bytes, num_bytes, num_bytes,
end + 1, &alloc_hint); end + 1, &alloc_hint);
cur_offset = end + 1; cur_offset = end + 1;
unlock_extent(&inode->io_tree, start, end, NULL); unlock_extent(&inode->io_tree, start, end, &cached_state);
if (ret) if (ret)
break; break;
} }
...@@ -2891,6 +2898,7 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod ...@@ -2891,6 +2898,7 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod
u64 start, u64 end, u64 block_start) u64 start, u64 end, u64 block_start)
{ {
struct extent_map *em; struct extent_map *em;
struct extent_state *cached_state = NULL;
int ret = 0; int ret = 0;
em = alloc_extent_map(); em = alloc_extent_map();
...@@ -2903,9 +2911,9 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod ...@@ -2903,9 +2911,9 @@ static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inod
em->block_start = block_start; em->block_start = block_start;
set_bit(EXTENT_FLAG_PINNED, &em->flags); set_bit(EXTENT_FLAG_PINNED, &em->flags);
lock_extent(&BTRFS_I(inode)->io_tree, start, end, NULL); lock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);
ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, false); ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, false);
unlock_extent(&BTRFS_I(inode)->io_tree, start, end, NULL); unlock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);
free_extent_map(em); free_extent_map(em);
return ret; return ret;
...@@ -2983,6 +2991,7 @@ static int relocate_one_page(struct inode *inode, struct file_ra_state *ra, ...@@ -2983,6 +2991,7 @@ static int relocate_one_page(struct inode *inode, struct file_ra_state *ra,
*/ */
cur = max(page_start, cluster->boundary[*cluster_nr] - offset); cur = max(page_start, cluster->boundary[*cluster_nr] - offset);
while (cur <= page_end) { while (cur <= page_end) {
struct extent_state *cached_state = NULL;
u64 extent_start = cluster->boundary[*cluster_nr] - offset; u64 extent_start = cluster->boundary[*cluster_nr] - offset;
u64 extent_end = get_cluster_boundary_end(cluster, u64 extent_end = get_cluster_boundary_end(cluster,
*cluster_nr) - offset; *cluster_nr) - offset;
...@@ -2998,13 +3007,15 @@ static int relocate_one_page(struct inode *inode, struct file_ra_state *ra, ...@@ -2998,13 +3007,15 @@ static int relocate_one_page(struct inode *inode, struct file_ra_state *ra,
goto release_page; goto release_page;
/* Mark the range delalloc and dirty for later writeback */ /* Mark the range delalloc and dirty for later writeback */
lock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end, NULL); lock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end,
&cached_state);
ret = btrfs_set_extent_delalloc(BTRFS_I(inode), clamped_start, ret = btrfs_set_extent_delalloc(BTRFS_I(inode), clamped_start,
clamped_end, 0, NULL); clamped_end, 0, &cached_state);
if (ret) { if (ret) {
clear_extent_bits(&BTRFS_I(inode)->io_tree, clear_extent_bit(&BTRFS_I(inode)->io_tree,
clamped_start, clamped_end, clamped_start, clamped_end,
EXTENT_LOCKED | EXTENT_BOUNDARY); EXTENT_LOCKED | EXTENT_BOUNDARY,
&cached_state);
btrfs_delalloc_release_metadata(BTRFS_I(inode), btrfs_delalloc_release_metadata(BTRFS_I(inode),
clamped_len, true); clamped_len, true);
btrfs_delalloc_release_extents(BTRFS_I(inode), btrfs_delalloc_release_extents(BTRFS_I(inode),
...@@ -3031,7 +3042,8 @@ static int relocate_one_page(struct inode *inode, struct file_ra_state *ra, ...@@ -3031,7 +3042,8 @@ static int relocate_one_page(struct inode *inode, struct file_ra_state *ra,
boundary_start, boundary_end, boundary_start, boundary_end,
EXTENT_BOUNDARY); EXTENT_BOUNDARY);
} }
unlock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end, NULL); unlock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end,
&cached_state);
btrfs_delalloc_release_extents(BTRFS_I(inode), clamped_len); btrfs_delalloc_release_extents(BTRFS_I(inode), clamped_len);
cur += clamped_len; cur += clamped_len;
......
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