btrfs: tree-log: factor out a clean_log_buffer helper
authorChristoph Hellwig <hch@lst.de>
Mon, 3 Apr 2023 14:03:55 +0000 (16:03 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 17 Apr 2023 16:01:22 +0000 (18:01 +0200)
The tree-log code has three almost identical copies for the accounting on
an extent_buffer that doesn't need to be written any more.  The only
difference is that walk_down_log_tree passed the bytenr used to find the
buffer instead of extent_buffer.start and calculates the length using the
nodesize, while the other two callers look at the extent_buffer.len
field that must always be equivalent to the nodesize.

Factor the code into a common helper.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/tree-log.c

index 9ab793b..f6c3f14 100644 (file)
@@ -2563,6 +2563,28 @@ static void unaccount_log_buffer(struct btrfs_fs_info *fs_info, u64 start)
        btrfs_put_block_group(cache);
 }
 
+static int clean_log_buffer(struct btrfs_trans_handle *trans,
+                           struct extent_buffer *eb)
+{
+       int ret;
+
+       btrfs_tree_lock(eb);
+       btrfs_clear_buffer_dirty(trans, eb);
+       wait_on_extent_buffer_writeback(eb);
+       btrfs_tree_unlock(eb);
+
+       if (trans) {
+               ret = btrfs_pin_reserved_extent(trans, eb->start, eb->len);
+               if (ret)
+                       return ret;
+               btrfs_redirty_list_add(trans->transaction, eb);
+       } else {
+               unaccount_log_buffer(eb->fs_info, eb->start);
+       }
+
+       return 0;
+}
+
 static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
                                   struct btrfs_root *root,
                                   struct btrfs_path *path, int *level,
@@ -2573,7 +2595,6 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
        u64 ptr_gen;
        struct extent_buffer *next;
        struct extent_buffer *cur;
-       u32 blocksize;
        int ret = 0;
 
        while (*level > 0) {
@@ -2593,7 +2614,6 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
                check.level = *level - 1;
                check.has_first_key = true;
                btrfs_node_key_to_cpu(cur, &check.first_key, path->slots[*level]);
-               blocksize = fs_info->nodesize;
 
                next = btrfs_find_create_tree_block(fs_info, bytenr,
                                                    btrfs_header_owner(cur),
@@ -2617,22 +2637,10 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
                                        return ret;
                                }
 
-                               btrfs_tree_lock(next);
-                               btrfs_clear_buffer_dirty(trans, next);
-                               wait_on_extent_buffer_writeback(next);
-                               btrfs_tree_unlock(next);
-
-                               if (trans) {
-                                       ret = btrfs_pin_reserved_extent(trans,
-                                                       bytenr, blocksize);
-                                       if (ret) {
-                                               free_extent_buffer(next);
-                                               return ret;
-                                       }
-                                       btrfs_redirty_list_add(
-                                               trans->transaction, next);
-                               } else {
-                                       unaccount_log_buffer(fs_info, bytenr);
+                               ret = clean_log_buffer(trans, next);
+                               if (ret) {
+                                       free_extent_buffer(next);
+                                       return ret;
                                }
                        }
                        free_extent_buffer(next);
@@ -2662,7 +2670,6 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
                                 struct btrfs_path *path, int *level,
                                 struct walk_control *wc)
 {
-       struct btrfs_fs_info *fs_info = root->fs_info;
        int i;
        int slot;
        int ret;
@@ -2682,27 +2689,9 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
                                return ret;
 
                        if (wc->free) {
-                               struct extent_buffer *next;
-
-                               next = path->nodes[*level];
-
-                               btrfs_tree_lock(next);
-                               btrfs_clear_buffer_dirty(trans, next);
-                               wait_on_extent_buffer_writeback(next);
-                               btrfs_tree_unlock(next);
-
-                               if (trans) {
-                                       ret = btrfs_pin_reserved_extent(trans,
-                                                    path->nodes[*level]->start,
-                                                    path->nodes[*level]->len);
-                                       if (ret)
-                                               return ret;
-                                       btrfs_redirty_list_add(trans->transaction,
-                                                              next);
-                               } else {
-                                       unaccount_log_buffer(fs_info,
-                                               path->nodes[*level]->start);
-                               }
+                               ret = clean_log_buffer(trans, path->nodes[*level]);
+                               if (ret)
+                                       return ret;
                        }
                        free_extent_buffer(path->nodes[*level]);
                        path->nodes[*level] = NULL;
@@ -2720,7 +2709,6 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
 static int walk_log_tree(struct btrfs_trans_handle *trans,
                         struct btrfs_root *log, struct walk_control *wc)
 {
-       struct btrfs_fs_info *fs_info = log->fs_info;
        int ret = 0;
        int wret;
        int level;
@@ -2762,26 +2750,8 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,
                         orig_level);
                if (ret)
                        goto out;
-               if (wc->free) {
-                       struct extent_buffer *next;
-
-                       next = path->nodes[orig_level];
-
-                       btrfs_tree_lock(next);
-                       btrfs_clear_buffer_dirty(trans, next);
-                       wait_on_extent_buffer_writeback(next);
-                       btrfs_tree_unlock(next);
-
-                       if (trans) {
-                               ret = btrfs_pin_reserved_extent(trans,
-                                               next->start, next->len);
-                               if (ret)
-                                       goto out;
-                               btrfs_redirty_list_add(trans->transaction, next);
-                       } else {
-                               unaccount_log_buffer(fs_info, next->start);
-                       }
-               }
+               if (wc->free)
+                       ret = clean_log_buffer(trans, path->nodes[orig_level]);
        }
 
 out: