Linux filesystem development
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Al Viro <viro@zeniv.linux.org.uk>
Cc: linux-fsdevel@vger.kernel.org,
	 Linus Torvalds <torvalds@linux-foundation.org>,
	Neil Brown <neilb@suse.de>, Miklos Szeredi <miklos@szeredi.hu>,
	 Jan Kara <jack@suse.cz>
Subject: Re: [PATCH 02/21] new helper: d_splice_alias_ops()
Date: Wed, 26 Feb 2025 09:28:27 +0100	[thread overview]
Message-ID: <20250226-qualm-auspuff-421e62f9e666@brauner> (raw)
In-Reply-To: <20250224212051.1756517-2-viro@zeniv.linux.org.uk>

On Mon, Feb 24, 2025 at 09:20:32PM +0000, Al Viro wrote:
> Uses of d_set_d_op() on live dentry can be very dangerous; it is going
> to be withdrawn and replaced with saner things.
> 
> The best way for a filesystem is to have the default dentry_operations
> set at mount time and be done with that - __d_alloc() will use that.
> 
> Currently there are two cases when d_set_d_op() is used on a live dentry -
> one is procfs, which has several genuinely different dentry_operations
> instances (different ->d_revalidate(), etc.) and another is
> simple_lookup(), where we would be better off without overriding ->d_op.
> 
> For procfs we have d_set_d_op() calls followed by d_splice_alias();
> provide a new helper (d_splice_alias_ops(inode, dentry, d_ops)) that would
> combine those two, and do the d_set_d_op() part while under ->d_lock.
> That eliminates one of the places where ->d_flags had been modified
> without holding ->d_lock; current behaviour is not racy, but the reasons
> for that are far too brittle.  Better move to uniform locking rules and
> simpler proof of correctness...
> 
> The next commit will convert procfs to use of that helper; it is not
> exported and won't be until somebody comes up with convincing modular
> user for it.
> 
> Again, the best approach is to have default ->d_op and let __d_alloc()
> do the right thing; filesystem _may_ need non-uniform ->d_op (procfs
> does), but there'd better be good reasons for that.
> 
> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
> ---

Reviewed-by: Christian Brauner <brauner@kernel.org>

>  fs/dcache.c            | 63 ++++++++++++++++++++++++------------------
>  include/linux/dcache.h |  3 ++
>  2 files changed, 39 insertions(+), 27 deletions(-)
> 
> diff --git a/fs/dcache.c b/fs/dcache.c
> index e3634916ffb9..c85efbda133a 100644
> --- a/fs/dcache.c
> +++ b/fs/dcache.c
> @@ -2641,7 +2641,8 @@ EXPORT_SYMBOL(__d_lookup_unhash_wake);
>  
>  /* inode->i_lock held if inode is non-NULL */
>  
> -static inline void __d_add(struct dentry *dentry, struct inode *inode)
> +static inline void __d_add(struct dentry *dentry, struct inode *inode,
> +			   const struct dentry_operations *ops)
>  {
>  	wait_queue_head_t *d_wait;
>  	struct inode *dir = NULL;
> @@ -2652,6 +2653,8 @@ static inline void __d_add(struct dentry *dentry, struct inode *inode)
>  		n = start_dir_add(dir);
>  		d_wait = __d_lookup_unhash(dentry);
>  	}
> +	if (unlikely(ops))
> +		d_set_d_op(dentry, ops);
>  	if (inode) {
>  		unsigned add_flags = d_flags_for_inode(inode);
>  		hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
> @@ -2683,7 +2686,7 @@ void d_add(struct dentry *entry, struct inode *inode)
>  		security_d_instantiate(entry, inode);
>  		spin_lock(&inode->i_lock);
>  	}
> -	__d_add(entry, inode);
> +	__d_add(entry, inode, NULL);
>  }
>  EXPORT_SYMBOL(d_add);
>  
> @@ -2981,30 +2984,8 @@ static int __d_unalias(struct dentry *dentry, struct dentry *alias)
>  	return ret;
>  }
>  
> -/**
> - * d_splice_alias - splice a disconnected dentry into the tree if one exists
> - * @inode:  the inode which may have a disconnected dentry
> - * @dentry: a negative dentry which we want to point to the inode.
> - *
> - * If inode is a directory and has an IS_ROOT alias, then d_move that in
> - * place of the given dentry and return it, else simply d_add the inode
> - * to the dentry and return NULL.
> - *
> - * If a non-IS_ROOT directory is found, the filesystem is corrupt, and
> - * we should error out: directories can't have multiple aliases.
> - *
> - * This is needed in the lookup routine of any filesystem that is exportable
> - * (via knfsd) so that we can build dcache paths to directories effectively.
> - *
> - * If a dentry was found and moved, then it is returned.  Otherwise NULL
> - * is returned.  This matches the expected return value of ->lookup.
> - *
> - * Cluster filesystems may call this function with a negative, hashed dentry.
> - * In that case, we know that the inode will be a regular file, and also this
> - * will only occur during atomic_open. So we need to check for the dentry
> - * being already hashed only in the final case.
> - */
> -struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
> +struct dentry *d_splice_alias_ops(struct inode *inode, struct dentry *dentry,
> +				  const struct dentry_operations *ops)
>  {
>  	if (IS_ERR(inode))
>  		return ERR_CAST(inode);
> @@ -3050,9 +3031,37 @@ struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
>  		}
>  	}
>  out:
> -	__d_add(dentry, inode);
> +	__d_add(dentry, inode, ops);
>  	return NULL;
>  }
> +
> +/**
> + * d_splice_alias - splice a disconnected dentry into the tree if one exists
> + * @inode:  the inode which may have a disconnected dentry
> + * @dentry: a negative dentry which we want to point to the inode.
> + *
> + * If inode is a directory and has an IS_ROOT alias, then d_move that in
> + * place of the given dentry and return it, else simply d_add the inode
> + * to the dentry and return NULL.
> + *
> + * If a non-IS_ROOT directory is found, the filesystem is corrupt, and
> + * we should error out: directories can't have multiple aliases.
> + *
> + * This is needed in the lookup routine of any filesystem that is exportable
> + * (via knfsd) so that we can build dcache paths to directories effectively.
> + *
> + * If a dentry was found and moved, then it is returned.  Otherwise NULL
> + * is returned.  This matches the expected return value of ->lookup.
> + *
> + * Cluster filesystems may call this function with a negative, hashed dentry.
> + * In that case, we know that the inode will be a regular file, and also this
> + * will only occur during atomic_open. So we need to check for the dentry
> + * being already hashed only in the final case.
> + */
> +struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
> +{
> +	return d_splice_alias_ops(inode, dentry, NULL);
> +}
>  EXPORT_SYMBOL(d_splice_alias);
>  
>  /*
> diff --git a/include/linux/dcache.h b/include/linux/dcache.h
> index 4afb60365675..f47f3a47d97b 100644
> --- a/include/linux/dcache.h
> +++ b/include/linux/dcache.h
> @@ -250,6 +250,9 @@ extern struct dentry * d_alloc_anon(struct super_block *);
>  extern struct dentry * d_alloc_parallel(struct dentry *, const struct qstr *,
>  					wait_queue_head_t *);
>  extern struct dentry * d_splice_alias(struct inode *, struct dentry *);
> +/* weird procfs mess; *NOT* exported */
> +extern struct dentry * d_splice_alias_ops(struct inode *, struct dentry *,
> +					  const struct dentry_operations *);
>  extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *);
>  extern bool d_same_name(const struct dentry *dentry, const struct dentry *parent,
>  			const struct qstr *name);
> -- 
> 2.39.5
> 

  reply	other threads:[~2025-02-26  8:28 UTC|newest]

Thread overview: 87+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-24  1:06 [RFC] dentry->d_flags locking Al Viro
2025-02-24  1:38 ` Al Viro
2025-02-24 10:35   ` Christian Brauner
2025-02-24 14:14     ` Al Viro
2025-02-24 21:20       ` [PATCH 01/21] procfs: kill ->proc_dops Al Viro
2025-02-24 21:20         ` [PATCH 02/21] new helper: d_splice_alias_ops() Al Viro
2025-02-26  8:28           ` Christian Brauner [this message]
2025-02-24 21:20         ` [PATCH 03/21] switch procfs from d_set_d_op() to d_splice_alias_ops() Al Viro
2025-02-26  8:29           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 04/21] fuse: no need for special dentry_operations for root dentry Al Viro
2025-02-26  8:29           ` Christian Brauner
2025-02-27 14:33           ` Miklos Szeredi
2025-02-24 21:20         ` [PATCH 05/21] new helper: set_default_d_op() Al Viro
2025-02-26  8:30           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 06/21] split d_flags calculation out of d_set_d_op() Al Viro
2025-02-26  8:31           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 07/21] set_default_d_op(): calculate the matching value for ->d_flags Al Viro
2025-02-26  8:33           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 08/21] simple_lookup(): just set DCACHE_DONTCACHE Al Viro
2025-02-26  8:34           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 09/21] make d_set_d_op() static Al Viro
2025-02-25 23:09           ` NeilBrown
2025-02-26  8:35           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 10/21] d_alloc_parallel(): set DCACHE_PAR_LOOKUP earlier Al Viro
2025-02-26  8:36           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 11/21] nsfs, pidfs: drop the pointless ->d_delete() Al Viro
2025-02-26  8:37           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 12/21] shmem: no dentry retention past the refcount reaching zero Al Viro
2025-02-26  8:38           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 13/21] devpts, sunrpc: don't bother with ->d_delete or ->d_op, for that matter Al Viro
2025-02-26  8:38           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 14/21] hostfs: don't bother with ->d_op Al Viro
2025-02-26  8:38           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 15/21] kill simple_dentry_operations Al Viro
2025-02-26  8:39           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 16/21] ramfs, hugetlbfs, mqueue: set DCACHE_DONTCACHE Al Viro
2025-02-25 23:25           ` NeilBrown
2025-02-25 23:36             ` Al Viro
2025-02-25 23:40               ` NeilBrown
2025-02-26  8:39           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 17/21] 9p: don't bother with always_delete_dentry Al Viro
2025-02-26  8:40           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 18/21] efivarfs: use DCACHE_DONTCACHE instead of always_delete_dentry() Al Viro
2025-02-26  8:40           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 19/21] debugfs: use DCACHE_DONTCACHE Al Viro
2025-02-26  8:40           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 20/21] configfs: " Al Viro
2025-02-26  8:40           ` Christian Brauner
2025-02-24 21:20         ` [PATCH 21/21] afs dynroot: " Al Viro
2025-02-26  8:41           ` Christian Brauner
2025-02-25 15:55         ` [PATCH 01/21] procfs: kill ->proc_dops Jan Kara
2025-02-25 23:30           ` NeilBrown
2025-02-25 23:56             ` Al Viro
2025-02-25 22:51         ` NeilBrown
2025-02-26  0:03           ` Al Viro
2025-02-26  8:25         ` Christian Brauner
2025-02-26  8:41       ` [RFC] dentry->d_flags locking Christian Brauner
2025-02-24 11:45 ` Christian Brauner
2025-06-11  7:50   ` [RFC][PATCHES v2] " Al Viro
2025-06-11  7:54     ` [PATCH v2 01/21] d_set_mounted(): we don't need to bump seqcount component of rename_lock Al Viro
2025-06-11  7:54       ` [PATCH v2 02/21] procfs: kill ->proc_dops Al Viro
2025-06-11  7:54       ` [PATCH v2 03/21] new helper: d_splice_alias_ops() Al Viro
2025-06-11  7:54       ` [PATCH v2 04/21] switch procfs from d_set_d_op() to d_splice_alias_ops() Al Viro
2025-06-11  7:54       ` [PATCH v2 05/21] fuse: no need for special dentry_operations for root dentry Al Viro
2025-06-11  7:54       ` [PATCH v2 06/21] new helper: set_default_d_op() Al Viro
2025-06-11  7:54       ` [PATCH v2 07/21] split d_flags calculation out of d_set_d_op() Al Viro
2025-06-11  7:54       ` [PATCH v2 08/21] correct the set of flags forbidden at d_set_d_op() time Al Viro
2025-06-11  9:49         ` Christian Brauner
2025-06-11  7:54       ` [PATCH v2 09/21] set_default_d_op(): calculate the matching value for ->d_flags Al Viro
2025-06-11  7:54       ` [PATCH v2 10/21] simple_lookup(): just set DCACHE_DONTCACHE Al Viro
2025-06-11  9:50         ` Christian Brauner
2025-06-11  7:54       ` [PATCH v2 11/21] make d_set_d_op() static Al Viro
2025-06-11  7:54       ` [PATCH v2 12/21] d_alloc_parallel(): set DCACHE_PAR_LOOKUP earlier Al Viro
2025-06-11  7:54       ` [PATCH v2 13/21] shmem: no dentry retention past the refcount reaching zero Al Viro
2025-06-11  7:54       ` [PATCH v2 14/21] devpts, sunrpc, hostfs: don't bother with ->d_op Al Viro
2025-06-11  9:50         ` Christian Brauner
2025-06-11  7:54       ` [PATCH v2 15/21] kill simple_dentry_operations Al Viro
2025-06-11  7:54       ` [PATCH v2 16/21] ramfs, hugetlbfs, mqueue: set DCACHE_DONTCACHE Al Viro
2025-06-11  7:54       ` [PATCH v2 17/21] 9p: don't bother with always_delete_dentry Al Viro
2025-06-11  7:54       ` [PATCH v2 18/21] efivarfs: use DCACHE_DONTCACHE instead of always_delete_dentry() Al Viro
2025-06-11  7:54       ` [PATCH v2 19/21] debugfs: use DCACHE_DONTCACHE Al Viro
2025-06-11  7:54       ` [PATCH v2 20/21] configfs: " Al Viro
2025-06-11  7:54       ` [PATCH v2 21/21] tracefs: set DCACHE_DONTCACHE Al Viro
2025-06-11  9:51         ` Christian Brauner
2025-06-11  9:47     ` [RFC][PATCHES v2] dentry->d_flags locking Christian Brauner
2025-02-24 14:07 ` [RFC] " Miklos Szeredi
2025-02-25 17:56   ` Al Viro

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=20250226-qualm-auspuff-421e62f9e666@brauner \
    --to=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    --cc=neilb@suse.de \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /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