Skip to contentSkip to conversation

NFS: Fix stale negative cache on failed nfs_mkdir()

PATCH Superseded Jimmy Zuber 1 participant 1 message Aug 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 NFS: Fix stale negative cache on failed nfs_mkdir()

Since commit 8376583b84a1 ("nfs: change mkdir inode_operation to return alternate dentry if needed"), nfs_mkdir() no longer drops the dentry on RPC failure. A negative cache value can then result in spurious ENOENT results for further operations within that directory, such as open or stat.

We observed this breaking some applications where an EEXIST on mkdir caused the application to assume the directory to exist, only to fail operations within that directory due to the retained negative cache entry. The other create paths, nfs_create() and nfs_mknod(), still drop the dentry on failure. This patch restores the previous behavior.

Fixes 8376583b84a1 ("nfs: change mkdir inode_operation to return alternate dentry if needed")
Cc [email protected] # v6.15+

---

We reproduced this against a local knfsd export over NFSv4.1. The export is mounted several times with "nosharecache" so that each mount acts as an independent client with its own dcache:

  # /etc/exports
  /srv/nfs 127.0.0.1(rw,sync,no_subtree_check,no_root_squash,fsid=0)
  for i in $(seq 0 5); do
      mount -t nfs4 -o vers=4.1,nosharecache,acdirmin=30,acdirmax=60 \
          127.0.0.1:/ /mnt/nfs$i
  done

For each of 40 new cache/<id> directories, every mount stats the directory to seed a negative entry, then all mounts race to mkdir it. The winner writes a file and renames it, and the losers that got EEXIST then read that file back. On a vanilla v6.17 kernel, the 200 loser reads were:

unpatched: read OK 0 / ENOENT 200
patched: read OK 200 / ENOENT 0

 fs/nfs/dir.c | 2 ++
 1 file changed, 2 insertions(+)
fs/nfs/dir.c +2 -0
index d812179..f0b493a 100644
@@ -2437,6 +2437,8 @@ struct dentry *nfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
 	trace_nfs_mkdir_enter(dir, dentry);
 	ret = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
 	trace_nfs_mkdir_exit(dir, dentry, PTR_ERR_OR_ZERO(ret));
+	if (IS_ERR(ret))
+		d_drop(dentry);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(nfs_mkdir);
Signature · 1 line
2.51.0