Skip to contentSkip to conversation

smb: client: add "noautomount" mount option to skip junction submounts

PATCH In review 星海昔涟 2 participants 2 messages Oct 10 – Oct 12

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

PATCH 1/1 smb: client: add "noautomount" mount option to skip junction submounts

When the cifs client walks into a name-surrogate reparse point that points to a directory and that it cannot handle locally (a plain NTFS junction, for instance), the dentry is marked DCACHE_NEED_AUTOMOUNT and the next traversal creates a separate CIFS submount for it (cifs_d_automount() -> cifs_do_automount()).

That machinery is what makes DFS referrals work, but for regular junctions every traversal pays for a new superblock and SMB session and leaves a mount behind. Scanning a tree that contains many junctions therefore floods the mount table: a single find(1) over a Windows share whose package cache holds 2014 junctions created 2014 CIFS submounts, which broke tools that enumerate mounts (a hosting panel rendering one entry per mount stopped responding). The submounts are also mostly
useless: reading through them typically fails with EACCES even when the very same path is perfectly readable through the parent mount.

None of the existing options turn this off: "nodfs" only skips DFS referral resolution (these junction submounts are not DFS), and "reparse=" selects the format used when creating new reparse points, not how existing ones are traversed.

Add a "noautomount" mount option. When set, CIFS_FATTR_JUNCTION inodes are not marked S_AUTOMOUNT, so junctions stay regular directories and the SMB server resolves the reparse target on access - which Windows does transparently for normal path traversal. The default is off, so behavior without the option is completely unchanged. Note that the option also disables automounting of DFS referrals, so it should not be used on DFS mounts.

Tested against a Windows 11 SMB 3.1.1 server (workgroup) sharing C: and
D: with 2014 junctions (package cache) plus several system junctions ("Documents and Settings", "Start Menu/Programs", NVIDIA plugin dirs):

- with "noautomount": deep tree scans create zero submounts, junction directories are listable through the parent mount, and sequential reads run at full speed (99 MB/s, identical to the unpatched module on the same hardware),
- without it: behavior is identical to the unpatched module (one submount per junction, "CIFS: Attempting to mount" per entry).

Assisted-by DeepSeek-V4-Flash
---
 Documentation/admin-guide/cifs/usage.rst | 11 +++++++++++
 fs/smb/client/cifsfs.c                   |  2 ++
 fs/smb/client/fs_context.c               |  4 ++++
 fs/smb/client/fs_context.h               |  2 ++
 fs/smb/client/inode.c                    | 13 ++++++++++++-
 5 files changed, 31 insertions(+), 1 deletion(-)
Documentation/admin-guide/cifs/usage.rst +11 -0
index d989ae5..e4904ac 100644
@@ -613,6 +613,17 @@ A partial list of the supported mount options follows:
 		server claims to support it.  This can help work around
 		a problem with parsing of DFS paths with Samba server
 		versions 3.0.24 and 3.0.25.
+  noautomount
+		Do not turn name-surrogate reparse points (e.g. Windows
+		NTFS junctions) pointing to a directory into submounts
+		on traversal.  Such entries are then exposed as regular
+		directories and the server resolves the reparse target
+		on access.  Without this option a deep scan of a tree
+		containing many junctions creates one CIFS submount per
+		junction, which floods the mount table with entries that
+		are usually useless and often not even listable.  Note
+		that this option also disables automounting of DFS
+		referrals, so it should not be used on DFS mounts.
   remount
 		remount the share (often used to change from ro to rw mounts
 		or vice versa)
fs/smb/client/cifsfs.c +2 -0
index 149becc..db74faa 100644
@@ -748,6 +748,8 @@ cifs_show_options(struct seq_file *s, struct dentry *root)
 		seq_puts(s, ",nounix");
 	if (sbflags & CIFS_MOUNT_NO_DFS)
 		seq_puts(s, ",nodfs");
+	if (cifs_sb->ctx->noautomount)
+		seq_puts(s, ",noautomount");
 	if (sbflags & CIFS_MOUNT_POSIX_PATHS)
 		seq_puts(s, ",posixpaths");
 	if (sbflags & CIFS_MOUNT_SET_UID)
fs/smb/client/fs_context.c +4 -0
index b6d1e06..ff03b65 100644
@@ -90,6 +90,7 @@ const struct fs_parameter_spec smb3_fs_parameters[] = {
 	fsparam_flag("nomapchars", Opt_nomapchars),
 	fsparam_flag_no("sfu", Opt_sfu),
 	fsparam_flag("nodfs", Opt_nodfs),
+	fsparam_flag("noautomount", Opt_noautomount),
 	fsparam_flag_no("posixpaths", Opt_posixpaths),
 	fsparam_flag_no("unix", Opt_unix),
 	fsparam_flag_no("linux", Opt_unix),
@@ -1321,6 +1322,9 @@ static int smb3_fs_context_parse_param(struct fs_context *fc,
 	case Opt_nodfs:
 		ctx->nodfs = 1;
 		break;
+	case Opt_noautomount:
+		ctx->noautomount = 1;
+		break;
 	case Opt_hard:
 		if (result.negated) {
 			if (ctx->retry == 1)
fs/smb/client/fs_context.h +2 -0
index 0b64fcb..a0ed492 100644
@@ -113,6 +113,7 @@ enum cifs_param {
 	Opt_nomapchars,
 	Opt_sfu,
 	Opt_nodfs,
+	Opt_noautomount,
 	Opt_posixpaths,
 	Opt_unix,
 	Opt_nocase,
@@ -277,6 +278,7 @@ struct smb3_fs_context {
 	bool mand_lock:1;  /* send mandatory not posix byte range lock reqs */
 	bool seal:1;       /* request transport encryption on share */
 	bool nodfs:1;      /* Do not request DFS, even if available */
+	bool noautomount:1; /* Do not automount junctions/reparse points */
 	bool local_lease:1; /* check leases only on local system, not remote */
 	bool noblocksnd:1;
 	bool noautotune:1;
fs/smb/client/inode.c +12 -1
index 4661407..95e60f0 100644
@@ -248,7 +248,18 @@ cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
 	}
 	spin_unlock(&inode->i_lock);
 
-	if (fattr->cf_flags & CIFS_FATTR_JUNCTION)
+	/*
+	 * With the "noautomount" mount option, do not turn junctions and
+	 * other name-surrogate reparse points into submounts; keep them as
+	 * regular directories instead and let the server resolve the
+	 * reparse target on access.  Scanning a tree that contains many
+	 * junctions (e.g. a package cache) otherwise creates a submount per
+	 * junction and floods the mount table with entries that are useless
+	 * and often not even listable.  Default behavior is unchanged.
+	 * Note that the option also affects DFS referral mounts, so it
+	 * should not be used on DFS shares.
+	 */
+	if (fattr->cf_flags & CIFS_FATTR_JUNCTION && !cifs_sb->ctx->noautomount)
 		inode->i_flags |= S_AUTOMOUNT;
 	if (inode_state_read_once(inode) & I_NEW) {
 		cifs_set_netfs_context(inode);
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 5 lines · 星海昔涟

None of the existing options turn this off: "nodfs" only skips DFS referral resolution (these junction submounts are not DFS), and "reparse=" selects the format used when creating new reparse points, not how existing ones are traversed.

Quoted 7 lines

Add a "noautomount" mount option. When set, CIFS_FATTR_JUNCTION inodes are not marked S_AUTOMOUNT, so junctions stay regular directories and the SMB server resolves the reparse target on access - which Windows does transparently for normal path traversal. The default is off, so behavior without the option is completely unchanged. Note that the option also disables automounting of DFS referrals, so it should not be used on DFS mounts.

Instead of introducing a new mount option, can't you use 'noserverino' to handle such cases? That is, if you mount the share with 'noserverino' mount option and the client finds a directory with a name-surrogate reparse point that it can't handle, then neither create fake attrs nor make them as automounts.

The original intent of marking those as automounts was to prevent inode collisions when accessing files from those directories and then leaving the parent mount still trusting inode numbers from the server, as way to keep hardlinks working on the parent mount.