* [RFC] weird use of dget_parent() in xfs_filestream_get_parent()
@ 2025-03-24 21:52 Al Viro
2025-03-25 5:50 ` Al Viro
2025-04-08 6:26 ` Christoph Hellwig
0 siblings, 2 replies; 3+ messages in thread
From: Al Viro @ 2025-03-24 21:52 UTC (permalink / raw)
To: Christoph Hellwig; +Cc: linux-fsdevel
Function in question:
static struct xfs_inode *
xfs_filestream_get_parent(
struct xfs_inode *ip)
{
struct inode *inode = VFS_I(ip), *dir = NULL;
struct dentry *dentry, *parent;
dentry = d_find_alias(inode);
if (!dentry)
goto out;
parent = dget_parent(dentry);
if (!parent)
goto out_dput;
dir = igrab(d_inode(parent));
dput(parent);
out_dput:
dput(dentry);
out:
return dir ? XFS_I(dir) : NULL;
}
1) dget_parent(dentry) never returns NULL; if you have IS_ROOT(dentry) you
get an equivalent of dget(dentry). What do you want returned in that
case?
2) why bother with dget_parent() in the first place? What's wrong with
spin_lock(&dentry->d_lock); // stabilizes ->d_parent
dir = igrab(dentry->d_parent->d_inode);
spin_unlock(&dentry->d_lock);
or, if you intended NULL for root,
spin_lock(&dentry->d_lock); // stabilizes ->d_parent
if (!IS_ROOT(dentry))
dir = igrab(dentry->d_parent->d_inode);
spin_unlock(&dentry->d_lock);
Is there anything subtle I'm missing here?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC] weird use of dget_parent() in xfs_filestream_get_parent()
2025-03-24 21:52 [RFC] weird use of dget_parent() in xfs_filestream_get_parent() Al Viro
@ 2025-03-25 5:50 ` Al Viro
2025-04-08 6:26 ` Christoph Hellwig
1 sibling, 0 replies; 3+ messages in thread
From: Al Viro @ 2025-03-25 5:50 UTC (permalink / raw)
To: Christoph Hellwig; +Cc: linux-fsdevel
On Mon, Mar 24, 2025 at 09:52:15PM +0000, Al Viro wrote:
> Function in question:
>
> static struct xfs_inode *
> xfs_filestream_get_parent(
> struct xfs_inode *ip)
> {
> struct inode *inode = VFS_I(ip), *dir = NULL;
> struct dentry *dentry, *parent;
>
> dentry = d_find_alias(inode);
> if (!dentry)
> goto out;
>
> parent = dget_parent(dentry);
> if (!parent)
> goto out_dput;
>
> dir = igrab(d_inode(parent));
> dput(parent);
>
> out_dput:
> dput(dentry);
> out:
> return dir ? XFS_I(dir) : NULL;
> }
>
> 1) dget_parent(dentry) never returns NULL; if you have IS_ROOT(dentry) you
> get an equivalent of dget(dentry). What do you want returned in that
> case?
>
> 2) why bother with dget_parent() in the first place? What's wrong with
> spin_lock(&dentry->d_lock); // stabilizes ->d_parent
> dir = igrab(dentry->d_parent->d_inode);
> spin_unlock(&dentry->d_lock);
>
> or, if you intended NULL for root,
> spin_lock(&dentry->d_lock); // stabilizes ->d_parent
> if (!IS_ROOT(dentry))
> dir = igrab(dentry->d_parent->d_inode);
> spin_unlock(&dentry->d_lock);
>
>
> Is there anything subtle I'm missing here?
For (2) there is - ->i_lock nests outside of ->d_lock, so igrab() under
any ->d_lock is not valid.
For (1) we provably can't get NULL - there are two returns in dget_parent():
if (!read_seqcount_retry(&dentry->d_seq, seq))
return ret;
and
spin_unlock(&ret->d_lock);
return ret;
and neither can return a NULL - we'd oops immediately prior to getting
there.
The same goes for xrep_findparent_from_dcache() - in
parent = dget_parent(dentry);
if (!parent)
goto out_dput;
ASSERT(parent->d_sb == sc->ip->i_mount->m_super);
pip = igrab(d_inode(parent));
dput(parent);
we need to use dget_parent(), but we really can't get a NULL from
it.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [RFC] weird use of dget_parent() in xfs_filestream_get_parent()
2025-03-24 21:52 [RFC] weird use of dget_parent() in xfs_filestream_get_parent() Al Viro
2025-03-25 5:50 ` Al Viro
@ 2025-04-08 6:26 ` Christoph Hellwig
1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2025-04-08 6:26 UTC (permalink / raw)
To: Al Viro; +Cc: Christoph Hellwig, linux-fsdevel
On Mon, Mar 24, 2025 at 09:52:15PM +0000, Al Viro wrote:
> 1) dget_parent(dentry) never returns NULL; if you have IS_ROOT(dentry) you
> get an equivalent of dget(dentry). What do you want returned in that
> case?
xfs_filestream_get_parent is only called for regular files. IS_ROOT can
probably only happen for partially connected trees on nfs exports, in which
case we'd want NULL;
>
> 2) why bother with dget_parent() in the first place? What's wrong with
> spin_lock(&dentry->d_lock); // stabilizes ->d_parent
> dir = igrab(dentry->d_parent->d_inode);
> spin_unlock(&dentry->d_lock);
>
> or, if you intended NULL for root,
> spin_lock(&dentry->d_lock); // stabilizes ->d_parent
> if (!IS_ROOT(dentry))
> dir = igrab(dentry->d_parent->d_inode);
> spin_unlock(&dentry->d_lock);
>
>
> Is there anything subtle I'm missing here?
Nothing really, except that this probably feelt like poking a bit too
much into internals.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-04-08 6:26 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-24 21:52 [RFC] weird use of dget_parent() in xfs_filestream_get_parent() Al Viro
2025-03-25 5:50 ` Al Viro
2025-04-08 6:26 ` Christoph Hellwig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).