Skip to content

Commit 6b0a63a

Browse files
josefbacikkdave
authored andcommitted
btrfs: add a cached state to extent_clear_unlock_delalloc
Now that we have the lock_extent tightly coupled with extent_clear_unlock_delalloc we can add a cached state to extent_clear_unlock_delalloc and benefit from skipping the extra lookup when we're doing cow. Reviewed-by: Goldwyn Rodrigues <rgoldwyn@suse.com> Signed-off-by: Josef Bacik <josef@toxicpanda.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 8325f41 commit 6b0a63a

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

fs/btrfs/extent_io.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,10 @@ noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
412412

413413
void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
414414
struct page *locked_page,
415+
struct extent_state **cached,
415416
u32 clear_bits, unsigned long page_ops)
416417
{
417-
clear_extent_bit(&inode->io_tree, start, end, clear_bits, NULL);
418+
clear_extent_bit(&inode->io_tree, start, end, clear_bits, cached);
418419

419420
__process_pages_contig(inode->vfs_inode.i_mapping, locked_page,
420421
start, end, page_ops);

fs/btrfs/extent_io.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct address_space;
2727
struct writeback_control;
2828
struct extent_io_tree;
2929
struct extent_map_tree;
30+
struct extent_state;
3031
struct btrfs_block_group;
3132
struct btrfs_fs_info;
3233
struct btrfs_inode;
@@ -352,6 +353,7 @@ void clear_extent_buffer_uptodate(struct extent_buffer *eb);
352353
void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end);
353354
void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
354355
struct page *locked_page,
356+
struct extent_state **cached,
355357
u32 bits_to_clear, unsigned long page_ops);
356358
int extent_invalidate_folio(struct extent_io_tree *tree,
357359
struct folio *folio, size_t offset);

fs/btrfs/inode.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,8 @@ static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 offset,
762762
return ret;
763763
}
764764

765-
free_extent_state(cached);
766-
extent_clear_unlock_delalloc(inode, offset, end, NULL, clear_flags,
765+
extent_clear_unlock_delalloc(inode, offset, end, NULL, &cached,
766+
clear_flags,
767767
PAGE_UNLOCK | PAGE_START_WRITEBACK |
768768
PAGE_END_WRITEBACK);
769769
return ret;
@@ -1154,6 +1154,7 @@ static void submit_one_async_extent(struct async_chunk *async_chunk,
11541154
struct btrfs_ordered_extent *ordered;
11551155
struct btrfs_key ins;
11561156
struct page *locked_page = NULL;
1157+
struct extent_state *cached = NULL;
11571158
struct extent_map *em;
11581159
int ret = 0;
11591160
u64 start = async_extent->start;
@@ -1194,7 +1195,7 @@ static void submit_one_async_extent(struct async_chunk *async_chunk,
11941195
goto done;
11951196
}
11961197

1197-
lock_extent(io_tree, start, end, NULL);
1198+
lock_extent(io_tree, start, end, &cached);
11981199

11991200
/* Here we're doing allocation and writeback of the compressed pages */
12001201
em = create_io_em(inode, start,
@@ -1229,7 +1230,7 @@ static void submit_one_async_extent(struct async_chunk *async_chunk,
12291230

12301231
/* Clear dirty, set writeback and unlock the pages. */
12311232
extent_clear_unlock_delalloc(inode, start, end,
1232-
NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
1233+
NULL, &cached, EXTENT_LOCKED | EXTENT_DELALLOC,
12331234
PAGE_UNLOCK | PAGE_START_WRITEBACK);
12341235
btrfs_submit_compressed_write(ordered,
12351236
async_extent->folios, /* compressed_folios */
@@ -1247,7 +1248,8 @@ static void submit_one_async_extent(struct async_chunk *async_chunk,
12471248
btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
12481249
mapping_set_error(inode->vfs_inode.i_mapping, -EIO);
12491250
extent_clear_unlock_delalloc(inode, start, end,
1250-
NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
1251+
NULL, &cached,
1252+
EXTENT_LOCKED | EXTENT_DELALLOC |
12511253
EXTENT_DELALLOC_NEW |
12521254
EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
12531255
PAGE_UNLOCK | PAGE_START_WRITEBACK |
@@ -1329,6 +1331,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
13291331
{
13301332
struct btrfs_root *root = inode->root;
13311333
struct btrfs_fs_info *fs_info = root->fs_info;
1334+
struct extent_state *cached = NULL;
13321335
u64 alloc_hint = 0;
13331336
u64 orig_start = start;
13341337
u64 num_bytes;
@@ -1429,7 +1432,8 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
14291432

14301433
ram_size = ins.offset;
14311434

1432-
lock_extent(&inode->io_tree, start, start + ram_size - 1, NULL);
1435+
lock_extent(&inode->io_tree, start, start + ram_size - 1,
1436+
&cached);
14331437

14341438
em = create_io_em(inode, start, ins.offset, /* len */
14351439
start, /* orig_start */
@@ -1441,7 +1445,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
14411445
BTRFS_ORDERED_REGULAR /* type */);
14421446
if (IS_ERR(em)) {
14431447
unlock_extent(&inode->io_tree, start,
1444-
start + ram_size - 1, NULL);
1448+
start + ram_size - 1, &cached);
14451449
ret = PTR_ERR(em);
14461450
goto out_reserve;
14471451
}
@@ -1453,7 +1457,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
14531457
BTRFS_COMPRESS_NONE);
14541458
if (IS_ERR(ordered)) {
14551459
unlock_extent(&inode->io_tree, start,
1456-
start + ram_size - 1, NULL);
1460+
start + ram_size - 1, &cached);
14571461
ret = PTR_ERR(ordered);
14581462
goto out_drop_extent_cache;
14591463
}
@@ -1493,7 +1497,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
14931497
page_ops |= PAGE_SET_ORDERED;
14941498

14951499
extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
1496-
locked_page,
1500+
locked_page, &cached,
14971501
EXTENT_LOCKED | EXTENT_DELALLOC,
14981502
page_ops);
14991503
if (num_bytes < cur_alloc_size)
@@ -1552,7 +1556,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
15521556
if (!locked_page)
15531557
mapping_set_error(inode->vfs_inode.i_mapping, ret);
15541558
extent_clear_unlock_delalloc(inode, orig_start, start - 1,
1555-
locked_page, 0, page_ops);
1559+
locked_page, NULL, 0, page_ops);
15561560
}
15571561

15581562
/*
@@ -1575,7 +1579,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
15751579
if (extent_reserved) {
15761580
extent_clear_unlock_delalloc(inode, start,
15771581
start + cur_alloc_size - 1,
1578-
locked_page,
1582+
locked_page, &cached,
15791583
clear_bits,
15801584
page_ops);
15811585
start += cur_alloc_size;
@@ -1590,7 +1594,7 @@ static noinline int cow_file_range(struct btrfs_inode *inode,
15901594
if (start < end) {
15911595
clear_bits |= EXTENT_CLEAR_DATA_RESV;
15921596
extent_clear_unlock_delalloc(inode, start, end, locked_page,
1593-
clear_bits, page_ops);
1597+
&cached, clear_bits, page_ops);
15941598
}
15951599
return ret;
15961600
}
@@ -2206,11 +2210,10 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
22062210
btrfs_put_ordered_extent(ordered);
22072211

22082212
extent_clear_unlock_delalloc(inode, cur_offset, nocow_end,
2209-
locked_page, EXTENT_LOCKED |
2210-
EXTENT_DELALLOC |
2213+
locked_page, &cached_state,
2214+
EXTENT_LOCKED | EXTENT_DELALLOC |
22112215
EXTENT_CLEAR_DATA_RESV,
22122216
PAGE_UNLOCK | PAGE_SET_ORDERED);
2213-
free_extent_state(cached_state);
22142217

22152218
cur_offset = extent_end;
22162219

@@ -2252,10 +2255,13 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
22522255
* we're not locked at this point.
22532256
*/
22542257
if (cur_offset < end) {
2255-
lock_extent(&inode->io_tree, cur_offset, end, NULL);
2258+
struct extent_state *cached = NULL;
2259+
2260+
lock_extent(&inode->io_tree, cur_offset, end, &cached);
22562261
extent_clear_unlock_delalloc(inode, cur_offset, end,
2257-
locked_page, EXTENT_LOCKED |
2258-
EXTENT_DELALLOC | EXTENT_DEFRAG |
2262+
locked_page, &cached,
2263+
EXTENT_LOCKED | EXTENT_DELALLOC |
2264+
EXTENT_DEFRAG |
22592265
EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
22602266
PAGE_START_WRITEBACK |
22612267
PAGE_END_WRITEBACK);

0 commit comments

Comments
 (0)