Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

nfs_instantiate(): prevent multiple aliases for directory inode

Since NFS allows open-by-fhandle, we have to cope with the possibility
of mkdir vs. open-by-guessed-handle races. A local filesystem could
decide what the inumber of the new object will be and insert a locked
inode with that inumber into icache _before_ the on-disk data structures
begin to look good and unlock it only once it has a dentry alias, so
that open-by-handle coming first would quietly fail and mkdir coming
first would have open-by-handle grab its dentry.

For NFS it's a non-starter - the icache key is server-supplied fhandle
and we do not get that until the object has been fully created on server.
We really have to deal with the possibility that open-by-handle gets
the in-core inode and attaches a dentry to it before mkdir does.

Solution: let nfs_mkdir() use d_splice_alias() to catch those. We can
* get an error. Just return it to our caller.
* get NULL - no preexisting dentry aliases, we'd just done what
d_add() would've done. Success.
* get a reference to preexisting alias. In that case the alias
had been moved in place of nfs_mkdir() argument (and hashed there), while
nfs_mkdir() argument is left unhashed negative. Which is just fine for
->mkdir() callers, all we need is to release the reference we'd got from
d_splice_alias() and report success.

Cc: Trond Myklebust <trond.myklebust@primarydata.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

Al Viro b0c6108e 877f919e

+6 -3
+6 -3
fs/nfs/dir.c
··· 1641 1641 struct dentry *parent = dget_parent(dentry); 1642 1642 struct inode *dir = d_inode(parent); 1643 1643 struct inode *inode; 1644 + struct dentry *d; 1644 1645 int error = -EACCES; 1645 1646 1646 1647 d_drop(dentry); ··· 1663 1662 goto out_error; 1664 1663 } 1665 1664 inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); 1666 - error = PTR_ERR(inode); 1667 - if (IS_ERR(inode)) 1665 + d = d_splice_alias(inode, dentry); 1666 + if (IS_ERR(d)) { 1667 + error = PTR_ERR(d); 1668 1668 goto out_error; 1669 - d_add(dentry, inode); 1669 + } 1670 + dput(d); 1670 1671 out: 1671 1672 dput(parent); 1672 1673 return 0;