diff mbox series

[29/44] btrfs: hold a ref for the root in btrfs_find_orphan_roots

Message ID 20200124143301.2186319-30-josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series Cleanup how we handle root refs, part 1 | expand

Commit Message

Josef Bacik Jan. 24, 2020, 2:32 p.m. UTC
We lookup roots for every orphan item we have, we need to hold a ref on
the root while we're doing this work.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/root-tree.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

David Sterba Feb. 5, 2020, 3:06 p.m. UTC | #1
On Fri, Jan 24, 2020 at 09:32:46AM -0500, Josef Bacik wrote:
> We lookup roots for every orphan item we have, we need to hold a ref on
> the root while we're doing this work.
> 
> Signed-off-by: Josef Bacik <josef@toxicpanda.com>
> ---
>  fs/btrfs/root-tree.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
> index 094a71c54fa1..25842527fd42 100644
> --- a/fs/btrfs/root-tree.c
> +++ b/fs/btrfs/root-tree.c
> @@ -257,6 +257,8 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
>  
>  		root = btrfs_get_fs_root(fs_info, &root_key, false);
>  		err = PTR_ERR_OR_ZERO(root);
> +		if (!err && !btrfs_grab_fs_root(root))
> +			err = -ENOENT;
>  		if (err && err != -ENOENT) {
>  			break;
>  		} else if (err == -ENOENT) {

It's hard to read and reason about where's the reference and where it's
not due to the combined options. There are some breaks and continues and
I think some refs might leak. I'll have another thought how it could be
restructured.

> @@ -288,6 +290,7 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
>  			set_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
>  			btrfs_add_dead_root(root);
>  		}
> +		btrfs_put_fs_root(root);
>  	}
>  
>  	btrfs_free_path(path);
> -- 
> 2.24.1
diff mbox series

Patch

diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
index 094a71c54fa1..25842527fd42 100644
--- a/fs/btrfs/root-tree.c
+++ b/fs/btrfs/root-tree.c
@@ -257,6 +257,8 @@  int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 
 		root = btrfs_get_fs_root(fs_info, &root_key, false);
 		err = PTR_ERR_OR_ZERO(root);
+		if (!err && !btrfs_grab_fs_root(root))
+			err = -ENOENT;
 		if (err && err != -ENOENT) {
 			break;
 		} else if (err == -ENOENT) {
@@ -288,6 +290,7 @@  int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 			set_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
 			btrfs_add_dead_root(root);
 		}
+		btrfs_put_fs_root(root);
 	}
 
 	btrfs_free_path(path);