Skip to content

Commit efe772d

Browse files
author
Al Viro
committed
follow_dotdot_rcu(): be lazy about changing nd->path
Change nd->path only after the loop is done and only in case we hadn't ended up finding ourselves in root. Same for NO_XDEV check. Don't recheck mount_lock on each step either. That separates the "check how far back do we need to go through the mount stack" logics from the rest of .. traversal. Note that the sequence for d_seq/d_inode here is * sample mount_lock seqcount ... * sample d_seq * fetch d_inode * verify mount_lock seqcount The last step makes sure that d_inode value we'd got matches d_seq - it dentry is guaranteed to have been a mountpoint through the entire thing, so its d_inode must have been stable. Signed-off-by: Al Viro <[email protected]>
1 parent 12487f3 commit efe772d

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

fs/namei.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,28 +1696,31 @@ static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
16961696
if (path_equal(&nd->path, &nd->root))
16971697
goto in_root;
16981698
if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
1699+
struct path path = nd->path;
1700+
unsigned seq;
1701+
16991702
while (1) {
1700-
struct mount *mnt = real_mount(nd->path.mnt);
1703+
struct mount *mnt = real_mount(path.mnt);
17011704
struct mount *mparent = mnt->mnt_parent;
17021705
struct dentry *mountpoint = mnt->mnt_mountpoint;
1703-
struct inode *inode = mountpoint->d_inode;
1704-
unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1705-
if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1706-
return ERR_PTR(-ECHILD);
1707-
if (&mparent->mnt == nd->path.mnt)
1706+
seq = read_seqcount_begin(&mountpoint->d_seq);
1707+
if (&mparent->mnt == path.mnt)
17081708
goto in_root;
1709-
if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1710-
return ERR_PTR(-ECHILD);
1711-
/* we know that mountpoint was pinned */
1712-
nd->path.dentry = mountpoint;
1713-
nd->path.mnt = &mparent->mnt;
1714-
nd->inode = inode;
1715-
nd->seq = seq;
1716-
if (path_equal(&nd->path, &nd->root))
1709+
path.dentry = mountpoint;
1710+
path.mnt = &mparent->mnt;
1711+
if (path_equal(&path, &nd->root))
17171712
goto in_root;
1718-
if (nd->path.dentry != nd->path.mnt->mnt_root)
1713+
if (path.dentry != path.mnt->mnt_root)
17191714
break;
17201715
}
1716+
if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1717+
return ERR_PTR(-ECHILD);
1718+
nd->path = path;
1719+
nd->inode = path.dentry->d_inode;
1720+
nd->seq = seq;
1721+
if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1722+
return ERR_PTR(-ECHILD);
1723+
/* we know that mountpoint was pinned */
17211724
}
17221725
old = nd->path.dentry;
17231726
parent = old->d_parent;
@@ -1729,6 +1732,8 @@ static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
17291732
return ERR_PTR(-ECHILD);
17301733
return parent;
17311734
in_root:
1735+
if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1736+
return ERR_PTR(-ECHILD);
17321737
if (unlikely(nd->flags & LOOKUP_BENEATH))
17331738
return ERR_PTR(-ECHILD);
17341739
return NULL;

0 commit comments

Comments
 (0)