btrfs: replace clearing extent buffer dirty bit with btrfs_clean_block
authorJosef Bacik <josef@toxicpanda.com>
Thu, 26 Jan 2023 21:00:56 +0000 (16:00 -0500)
committerDavid Sterba <dsterba@suse.com>
Wed, 15 Feb 2023 18:38:53 +0000 (19:38 +0100)
Now that we're passing in the trans into btrfs_clean_tree_block, we can
easily roll in the handling of the !trans case and replace all
occurrences of

if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags))
clear_extent_buffer_dirty(eb);

with

btrfs_tree_lock(eb);
btrfs_clean_tree_block(eb);
btrfs_tree_unlock(eb);

We need the lock because if we are actually dirty we need to make sure
we aren't racing with anything that's starting writeout currently.  This
also makes sure that we're accounting fs_info->dirty_metadata_bytes
appropriately.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/disk-io.c
fs/btrfs/tree-log.c

index 0b5e63929a47695376cfcbf006f474ccd34aba34..be9bb89723972df8ff4f16c6437cb7d85f38b940 100644 (file)
@@ -872,7 +872,7 @@ void btrfs_clean_tree_block(struct btrfs_trans_handle *trans,
                            struct extent_buffer *buf)
 {
        struct btrfs_fs_info *fs_info = buf->fs_info;
-       if (btrfs_header_generation(buf) == trans->transid) {
+       if (!trans || btrfs_header_generation(buf) == trans->transid) {
                btrfs_assert_tree_write_locked(buf);
 
                if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
@@ -4995,11 +4995,12 @@ static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
                        start += fs_info->nodesize;
                        if (!eb)
                                continue;
+
+                       btrfs_tree_lock(eb);
                        wait_on_extent_buffer_writeback(eb);
+                       btrfs_clean_tree_block(NULL, eb);
+                       btrfs_tree_unlock(eb);
 
-                       if (test_and_clear_bit(EXTENT_BUFFER_DIRTY,
-                                              &eb->bflags))
-                               clear_extent_buffer_dirty(eb);
                        free_extent_buffer_stale(eb);
                }
        }
index e683fbb9bb304d608575c63d6b68322a0ab419e3..a74acb341ef2625329130fa477e935438ff635de 100644 (file)
@@ -2623,11 +2623,12 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
                                        return ret;
                                }
 
+                               btrfs_tree_lock(next);
+                               btrfs_clean_tree_block(trans, next);
+                               btrfs_wait_tree_block_writeback(next);
+                               btrfs_tree_unlock(next);
+
                                if (trans) {
-                                       btrfs_tree_lock(next);
-                                       btrfs_clean_tree_block(trans, next);
-                                       btrfs_wait_tree_block_writeback(next);
-                                       btrfs_tree_unlock(next);
                                        ret = btrfs_pin_reserved_extent(trans,
                                                        bytenr, blocksize);
                                        if (ret) {
@@ -2637,8 +2638,6 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
                                        btrfs_redirty_list_add(
                                                trans->transaction, next);
                                } else {
-                                       if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
-                                               clear_extent_buffer_dirty(next);
                                        unaccount_log_buffer(fs_info, bytenr);
                                }
                        }
@@ -2693,11 +2692,12 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
 
                                next = path->nodes[*level];
 
+                               btrfs_tree_lock(next);
+                               btrfs_clean_tree_block(trans, next);
+                               btrfs_wait_tree_block_writeback(next);
+                               btrfs_tree_unlock(next);
+
                                if (trans) {
-                                       btrfs_tree_lock(next);
-                                       btrfs_clean_tree_block(trans, next);
-                                       btrfs_wait_tree_block_writeback(next);
-                                       btrfs_tree_unlock(next);
                                        ret = btrfs_pin_reserved_extent(trans,
                                                     path->nodes[*level]->start,
                                                     path->nodes[*level]->len);
@@ -2706,9 +2706,6 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
                                        btrfs_redirty_list_add(trans->transaction,
                                                               next);
                                } else {
-                                       if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
-                                               clear_extent_buffer_dirty(next);
-
                                        unaccount_log_buffer(fs_info,
                                                path->nodes[*level]->start);
                                }
@@ -2776,19 +2773,18 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,
 
                        next = path->nodes[orig_level];
 
+                       btrfs_tree_lock(next);
+                       btrfs_clean_tree_block(trans, next);
+                       btrfs_wait_tree_block_writeback(next);
+                       btrfs_tree_unlock(next);
+
                        if (trans) {
-                               btrfs_tree_lock(next);
-                               btrfs_clean_tree_block(trans, next);
-                               btrfs_wait_tree_block_writeback(next);
-                               btrfs_tree_unlock(next);
                                ret = btrfs_pin_reserved_extent(trans,
                                                next->start, next->len);
                                if (ret)
                                        goto out;
                                btrfs_redirty_list_add(trans->transaction, next);
                        } else {
-                               if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
-                                       clear_extent_buffer_dirty(next);
                                unaccount_log_buffer(fs_info, next->start);
                        }
                }