diff mbox series

[32/44] btrfs: hold a ref on the root in get_subvol_name_from_objectid

Message ID 20200124143301.2186319-33-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 the name of a subvol which means we'll cross into different
roots.  Hold a ref while we're doing the look ups in the fs_root we're
searching.

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

Comments

David Sterba Feb. 5, 2020, 3:19 p.m. UTC | #1
On Fri, Jan 24, 2020 at 09:32:49AM -0500, Josef Bacik wrote:
> We lookup the name of a subvol which means we'll cross into different
> roots.  Hold a ref while we're doing the look ups in the fs_root we're
> searching.
> 
> Signed-off-by: Josef Bacik <josef@toxicpanda.com>
> ---
>  fs/btrfs/super.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
> index 3118bc01321e..5c3a1b7de6ee 100644
> --- a/fs/btrfs/super.c
> +++ b/fs/btrfs/super.c
> @@ -1101,6 +1101,10 @@ static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
>  			ret = PTR_ERR(fs_root);
>  			goto err;
>  		}
> +		if (!btrfs_grab_fs_root(fs_root)) {
> +			ret = -ENOENT;
> +			goto err;
> +		}
>  
>  		/*
>  		 * Walk up the filesystem tree by inode refs until we hit the
> @@ -1113,13 +1117,16 @@ static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
>  
>  			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
>  			if (ret < 0) {
> +				btrfs_put_fs_root(fs_root);
>  				goto err;
>  			} else if (ret > 0) {
>  				ret = btrfs_previous_item(fs_root, path, dirid,
>  							  BTRFS_INODE_REF_KEY);
>  				if (ret < 0) {
> +					btrfs_put_fs_root(fs_root);
>  					goto err;
>  				} else if (ret > 0) {
> +					btrfs_put_fs_root(fs_root);
>  					ret = -ENOENT;
>  					goto err;
>  				}
> @@ -1136,6 +1143,7 @@ static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
>  			ptr -= len + 1;
>  			if (ptr < name) {
>  				ret = -ENAMETOOLONG;
> +				btrfs_put_fs_root(fs_root);
>  				goto err;
>  			}
>  			read_extent_buffer(path->nodes[0], ptr + 1,
> @@ -1143,6 +1151,7 @@ static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
>  			ptr[0] = '/';
>  			btrfs_release_path(path);
>  		}
> +		btrfs_put_fs_root(fs_root);
>  	}
>  
>  	btrfs_free_path(path);

All the put_fs_root before goto err can and should be merged into the
exit block that's already there.

> -- 
> 2.24.1
diff mbox series

Patch

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 3118bc01321e..5c3a1b7de6ee 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -1101,6 +1101,10 @@  static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
 			ret = PTR_ERR(fs_root);
 			goto err;
 		}
+		if (!btrfs_grab_fs_root(fs_root)) {
+			ret = -ENOENT;
+			goto err;
+		}
 
 		/*
 		 * Walk up the filesystem tree by inode refs until we hit the
@@ -1113,13 +1117,16 @@  static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
 
 			ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
 			if (ret < 0) {
+				btrfs_put_fs_root(fs_root);
 				goto err;
 			} else if (ret > 0) {
 				ret = btrfs_previous_item(fs_root, path, dirid,
 							  BTRFS_INODE_REF_KEY);
 				if (ret < 0) {
+					btrfs_put_fs_root(fs_root);
 					goto err;
 				} else if (ret > 0) {
+					btrfs_put_fs_root(fs_root);
 					ret = -ENOENT;
 					goto err;
 				}
@@ -1136,6 +1143,7 @@  static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
 			ptr -= len + 1;
 			if (ptr < name) {
 				ret = -ENAMETOOLONG;
+				btrfs_put_fs_root(fs_root);
 				goto err;
 			}
 			read_extent_buffer(path->nodes[0], ptr + 1,
@@ -1143,6 +1151,7 @@  static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
 			ptr[0] = '/';
 			btrfs_release_path(path);
 		}
+		btrfs_put_fs_root(fs_root);
 	}
 
 	btrfs_free_path(path);