public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.cz>
To: Josef Bacik <josef@toxicpanda.com>
Cc: kernel-team@fb.com, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 18/44] btrfs: hold a ref on the root in btrfs_search_path_in_tree_user
Date: Wed, 5 Feb 2020 15:37:18 +0100	[thread overview]
Message-ID: <20200205143718.GM2654@twin.jikos.cz> (raw)
In-Reply-To: <20200124143301.2186319-19-josef@toxicpanda.com>

On Fri, Jan 24, 2020 at 09:32:35AM -0500, Josef Bacik wrote:
> We can wander into a different root, so grab a ref on the root we look
> up.  Later on we make root = fs_info->tree_root so we need this separate
> out label to make sure we do the right cleanup only in the case we're
> looking up a different root.
> 
> Signed-off-by: Josef Bacik <josef@toxicpanda.com>
> ---
>  fs/btrfs/ioctl.c | 28 ++++++++++++++++++----------
>  1 file changed, 18 insertions(+), 10 deletions(-)
> 
> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
> index c721b4fce1c0..5fffa1b6f685 100644
> --- a/fs/btrfs/ioctl.c
> +++ b/fs/btrfs/ioctl.c
> @@ -2432,6 +2432,10 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
>  			ret = PTR_ERR(root);
>  			goto out;
>  		}
> +		if (!btrfs_grab_fs_root(root)) {
> +			ret = -ENOENT;
> +			goto out;
> +		}
>  
>  		key.objectid = dirid;
>  		key.type = BTRFS_INODE_REF_KEY;
> @@ -2439,15 +2443,15 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
>  		while (1) {
>  			ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
>  			if (ret < 0) {
> -				goto out;
> +				goto out_put;
>  			} else if (ret > 0) {
>  				ret = btrfs_previous_item(root, path, dirid,
>  							  BTRFS_INODE_REF_KEY);
>  				if (ret < 0) {
> -					goto out;
> +					goto out_put;
>  				} else if (ret > 0) {
>  					ret = -ENOENT;
> -					goto out;
> +					goto out_put;
>  				}
>  			}
>  
> @@ -2461,7 +2465,7 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
>  			total_len += len + 1;
>  			if (ptr < args->path) {
>  				ret = -ENAMETOOLONG;
> -				goto out;
> +				goto out_put;
>  			}
>  
>  			*(ptr + len) = '/';
> @@ -2472,10 +2476,10 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
>  			ret = btrfs_previous_item(root, path, dirid,
>  						  BTRFS_INODE_ITEM_KEY);
>  			if (ret < 0) {
> -				goto out;
> +				goto out_put;
>  			} else if (ret > 0) {
>  				ret = -ENOENT;
> -				goto out;
> +				goto out_put;
>  			}
>  
>  			leaf = path->nodes[0];
> @@ -2483,26 +2487,26 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
>  			btrfs_item_key_to_cpu(leaf, &key2, slot);
>  			if (key2.objectid != dirid) {
>  				ret = -ENOENT;
> -				goto out;
> +				goto out_put;
>  			}
>  
>  			temp_inode = btrfs_iget(sb, &key2, root);
>  			if (IS_ERR(temp_inode)) {
>  				ret = PTR_ERR(temp_inode);
> -				goto out;
> +				goto out_put;
>  			}
>  			ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC);
>  			iput(temp_inode);
>  			if (ret) {
>  				ret = -EACCES;
> -				goto out;
> +				goto out_put;
>  			}
>  
>  			if (key.offset == upper_limit.objectid)
>  				break;
>  			if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
>  				ret = -EACCES;
> -				goto out;
> +				goto out_put;
>  			}
>  
>  			btrfs_release_path(path);
> @@ -2513,6 +2517,7 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
>  
>  		memmove(args->path, ptr, total_len);
>  		args->path[total_len] = '\0';
> +		btrfs_put_fs_root(root);
>  		btrfs_release_path(path);
>  	}
>  
> @@ -2551,6 +2556,9 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
>  out:
>  	btrfs_free_path(path);
>  	return ret;
> +out_put:
> +	btrfs_put_fs_root(root);
> +	goto out;

That's the ugly goto pattern we'd rather not use

I see a simple way to merge the gotos to one: set root to NULL after the
proper btrfs_put_fs_root (next to the memmove call above), and do the
error btrfs_put_fs_root before free path.

There's one catch, 'root' is used to hold tree_root that's only passed
to search slot, but that can be simplified so 'root' is always the
referenced root.

Incremental diff:

---
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2518,15 +2518,15 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
                memmove(args->path, ptr, total_len);
                args->path[total_len] = '\0';
                btrfs_put_fs_root(root);
+               root = NULL;
                btrfs_release_path(path);
        }
 
        /* Get the bottom subvolume's name from ROOT_REF */
-       root = fs_info->tree_root;
        key.objectid = treeid;
        key.type = BTRFS_ROOT_REF_KEY;
        key.offset = args->treeid;
-       ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+       ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
        if (ret < 0) {
                goto out;
        } else if (ret > 0) {
@@ -2553,12 +2553,11 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
        read_extent_buffer(leaf, args->name, item_off, item_len);
        args->name[item_len] = 0;
 
+out_put:
+       btrfs_put_fs_root(root);
 out:
        btrfs_free_path(path);
        return ret;
-out_put:
-       btrfs_put_fs_root(root);
-       goto out;
 }
---

  reply	other threads:[~2020-02-05 14:37 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-24 14:32 [PATCH 00/44][v5] Cleanup how we handle root refs, part 1 Josef Bacik
2020-01-24 14:32 ` [PATCH 01/44] btrfs: push __setup_root into btrfs_alloc_root Josef Bacik
2020-02-04 22:05   ` David Sterba
2020-01-24 14:32 ` [PATCH 02/44] btrfs: move fs root init stuff into btrfs_init_fs_root Josef Bacik
2020-01-24 14:32 ` [PATCH 03/44] btrfs: make btrfs_find_orphan_roots use btrfs_get_fs_root Josef Bacik
2020-01-24 14:32 ` [PATCH 04/44] btrfs: export and use btrfs_read_tree_root Josef Bacik
2020-01-24 14:32 ` [PATCH 05/44] btrfs: make relocation use btrfs_read_tree_root() Josef Bacik
2020-01-24 14:32 ` [PATCH 06/44] btrfs: kill btrfs_read_fs_root Josef Bacik
2020-01-24 14:32 ` [PATCH 07/44] btrfs: kill the btrfs_read_fs_root_no_name helper Josef Bacik
2020-01-24 14:32 ` [PATCH 08/44] btrfs: make the fs root init functions static Josef Bacik
2020-01-24 14:32 ` [PATCH 09/44] btrfs: handle NULL roots in btrfs_put/btrfs_grab_fs_root Josef Bacik
2020-01-24 14:32 ` [PATCH 10/44] btrfs: hold a ref on fs roots while they're in the radix tree Josef Bacik
2020-01-24 14:32 ` [PATCH 11/44] btrfs: hold a ref on the root in resolve_indirect_ref Josef Bacik
2020-01-24 14:32 ` [PATCH 12/44] btrfs: hold a root ref in btrfs_get_dentry Josef Bacik
2020-01-24 14:32 ` [PATCH 13/44] btrfs: hold a ref on the root in __btrfs_run_defrag_inode Josef Bacik
2020-01-24 14:32 ` [PATCH 14/44] btrfs: hold a ref on the root in fixup_tree_root_location Josef Bacik
2020-01-24 14:32 ` [PATCH 15/44] btrfs: hold a ref on the root in create_subvol Josef Bacik
2020-01-24 14:32 ` [PATCH 16/44] btrfs: hold a ref on the root in search_ioctl Josef Bacik
2020-01-24 14:32 ` [PATCH 17/44] btrfs: hold a ref on the root in btrfs_search_path_in_tree Josef Bacik
2020-02-05 14:30   ` David Sterba
2020-01-24 14:32 ` [PATCH 18/44] btrfs: hold a ref on the root in btrfs_search_path_in_tree_user Josef Bacik
2020-02-05 14:37   ` David Sterba [this message]
2020-01-24 14:32 ` [PATCH 19/44] btrfs: hold a ref on the root in btrfs_ioctl_get_subvol_info Josef Bacik
2020-01-24 14:32 ` [PATCH 20/44] btrfs: hold ref on root in btrfs_ioctl_default_subvol Josef Bacik
2020-01-24 14:32 ` [PATCH 21/44] btrfs: hold a ref on the root in build_backref_tree Josef Bacik
2020-02-05 14:46   ` David Sterba
2020-01-24 14:32 ` [PATCH 22/44] btrfs: hold a ref on the root in prepare_to_merge Josef Bacik
2020-02-05 14:52   ` David Sterba
2020-01-24 14:32 ` [PATCH 23/44] btrfs: hold a ref on the root in merge_reloc_roots Josef Bacik
2020-01-24 14:32 ` [PATCH 24/44] btrfs: hold a ref on the root in record_reloc_root_in_trans Josef Bacik
2020-01-24 14:32 ` [PATCH 25/44] btrfs: hold a ref on the root in find_data_references Josef Bacik
2020-01-24 14:32 ` [PATCH 26/44] btrfs: hold a ref on the root in create_reloc_inode Josef Bacik
2020-01-24 14:32 ` [PATCH 27/44] btrfs: hold a ref on the root in btrfs_recover_relocation Josef Bacik
2020-02-05 15:00   ` David Sterba
2020-02-06 16:26   ` David Sterba
2020-02-06 16:30     ` Josef Bacik
2020-01-24 14:32 ` [PATCH 28/44] btrfs: push grab_fs_root into read_fs_root Josef Bacik
2020-01-24 14:32 ` [PATCH 29/44] btrfs: hold a ref for the root in btrfs_find_orphan_roots Josef Bacik
2020-02-05 15:06   ` David Sterba
2020-01-24 14:32 ` [PATCH 30/44] btrfs: hold a ref on the root in scrub_print_warning_inode Josef Bacik
2020-01-24 14:32 ` [PATCH 31/44] btrfs: hold a ref on the root in btrfs_ioctl_send Josef Bacik
2020-02-05 15:16   ` David Sterba
2020-01-24 14:32 ` [PATCH 32/44] btrfs: hold a ref on the root in get_subvol_name_from_objectid Josef Bacik
2020-02-05 15:19   ` David Sterba
2020-02-06 15:24     ` [PATCH][v2] " Josef Bacik
2020-01-24 14:32 ` [PATCH 33/44] btrfs: hold a ref on the root in create_pending_snapshot Josef Bacik
2020-01-24 14:32 ` [PATCH 34/44] btrfs: hold a ref on the root in btrfs_recover_log_trees Josef Bacik
2020-01-24 14:32 ` [PATCH 35/44] btrfs: hold a ref on the root in btrfs_check_uuid_tree_entry Josef Bacik
2020-01-24 14:32 ` [PATCH 36/44] btrfs: move free_fs_info Josef Bacik
2020-01-24 14:52   ` Nikolay Borisov
2020-01-24 14:32 ` [PATCH 37/44] btrfs: hold a ref on the root in open_ctree Josef Bacik
2020-01-24 14:32 ` [PATCH 38/44] btrfs: use btrfs_put_fs_root to free roots always Josef Bacik
2020-01-24 14:32 ` [PATCH 39/44] btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root Josef Bacik
2020-01-24 14:32 ` [PATCH 40/44] btrfs: free more things in btrfs_free_fs_info Josef Bacik
2020-01-24 14:32 ` [PATCH 41/44] btrfs: move fs_info init work into it's own helper function Josef Bacik
2020-01-24 14:32 ` [PATCH 42/44] btrfs: make the init of static elements in fs_info separate Josef Bacik
2020-01-24 14:33 ` [PATCH 43/44] btrfs: add a leak check for roots Josef Bacik
2020-01-24 14:33 ` [PATCH 44/44] btrfs: rename btrfs_put_fs_root and btrfs_grab_fs_root Josef Bacik
2020-02-05 15:44   ` David Sterba
2020-02-04 23:48 ` [PATCH 00/44][v5] Cleanup how we handle root refs, part 1 David Sterba
2020-02-05 15:48 ` David Sterba
2020-02-06 16:59   ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200205143718.GM2654@twin.jikos.cz \
    --to=dsterba@suse.cz \
    --cc=josef@toxicpanda.com \
    --cc=kernel-team@fb.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox