Skip to contentSkip to conversation

smb: client: remount reconfigure option fixes

PATCH Superseded [email protected] 2 participants 19 messages Aug 13 – Aug 18

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

cover smb: client: remount reconfigure option fixes

From: Rajasi Mandal <[email protected]>

This series makes several mount options actually take effect (or be cleanly rejected) on remount instead of being silently ignored, and fixes the option-comparison baseline so a bare 'mount -o remount' is neither wrongly rejected nor allowed to clobber live runtime state.

Overview:
1: build the reconfigure baseline from the live mount (a private,

     runtime-synced dup of cifs_sb->ctx) instead of init defaults.

2: reject connection-tied options that cannot change on remount,
using that baseline so bare remounts and /proc/mounts replays are still accepted.
3: propagate retrans to the live server on remount. 4: reject cache=ro / cache=singleclient changes on remount. 5: apply rasize on remount.
6: move struct tcon_list to cifsglob.h (no functional change). 7: make nolease take effect on remount and evict lease-bearing
cached state.

Changes since the previous posting ("[PATCH RESEND 0/9]"):
- Folded the two "review fixes" commits into the patches they fix; the series is now 7 self-contained, bisectable patches.
- Patch 1: do not fabricate no_linux_ext (mirrors the existing nopersistent handling); sync the server-shared display fields into the baseline so a bare remount on a shared connection is not wrongly rejected.
- Patch 7: invalidate_all_cached_dirs_sb() counts tcons and allocates with GFP_KERNEL outside tlink_tree_lock instead of a per-tcon GFP_ATOMIC allocation that could fail mid-walk.

Known pre-existing issues intentionally NOT addressed here (each is called out in the relevant commit message):
- cifs_sb->ctx is read locklessly while smb3_reconfigure() overwrites it in-place, so a /proc/mounts read racing a remount of the same mount can observe a torn pointer. This predates the series; the proper fix (RCU-style ctx replacement) is left as its own change. (Patch 1)
- tcon->unix_ext shares a bitfield word with other lockless writers; theoretical word-tearing only. (Patch 1)
- Explicit 'mount -o remount,port=<active-default>' is rejected; not reachable via tooling since port is not in /proc/mounts. (Patch 2)
- cached_dir_lease_break() updates tcon->tc_count without tc_lock; cfids_laundromat_worker() can resurrect a tcon from tc_count 0 while cifs_put_tcon() tears it down; and the cifs_debug dirs knob invalidates with close_handles=false. All pre-existing and unrelated to nolease. (Patch 7)

Testing:
- checkpatch --strict clean (bar one pre-existing CamelCase CHECK on

    ses->domainName); each patch builds standalone (bisectable).

- Functional testing on Azure Files (SMB 3.1.1): a bare remount preserves the full /proc/mounts option set; retrans/nolease/rasize apply on remount; vers / echo_interval and other non-reconfigurable changes are rejected with -EINVAL; a remount replaying a server-displayed option is accepted; I/O works and dmesg is clean.

Rajasi Mandal (7):
smb: client: sync runtime state into ctx on reconfigure
smb: client: block non-reconfigurable option changes on remount
smb: client: sync retrans on remount
smb: client: block cache=ro and cache=singleclient on remount
smb: client: apply rasize on remount
smb: client: move struct tcon_list to cifsglob.h
smb: client: allow nolease option to be reconfigured on remount

 fs/smb/client/cached_dir.c |  83 ++++++-
 fs/smb/client/cached_dir.h |   4 +-
 fs/smb/client/cifs_debug.c |   2 +-
 fs/smb/client/cifs_swn.h   |  14 +-
 fs/smb/client/cifsfs.c     |   6 +-
 fs/smb/client/cifsglob.h   |   7 +-
 fs/smb/client/connect.c    |   4 +-
 fs/smb/client/file.c       |   2 +-
 fs/smb/client/fs_context.c | 446 ++++++++++++++++++++++++++++++++++++-
 fs/smb/client/misc.c       |   5 -
 fs/smb/client/smb1ops.c    |   7 +-
 fs/smb/client/smb2ops.c    |   2 +-
 fs/smb/client/smb2pdu.c    |  13 +-
 fs/smb/client/trace.h      |   2 +
 14 files changed, 567 insertions(+), 30 deletions(-)

base-commit: c0a27675eaf08255017b3cabc28c99c0cd71f468

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/7 smb: client: sync runtime state into ctx on reconfigure

From: Rajasi Mandal <[email protected]>

smb3_init_fs_context() builds a fresh context from init defaults on every call, including remount. Many options shown by cifs_show_options() come from tcon/server/ses rather than ctx, so those defaults don't reflect the live mount and make a poor baseline for deciding what changed on remount.

On reconfigure, duplicate the live cifs_sb->ctx into a private context and sync it with runtime state via a new smb3_sync_ctx_from_runtime() so the baseline matches what /proc/mounts displays. This mirrors libmount (util-linux), which on remount replays the current /proc/mounts options back to the kernel: the baseline must equal the displayed state, or a bare 'mount -o remount' looks like a change and is rejected. libmount isn't present everywhere, but the kernel must accept such a replay regardless. Syncing into the private copy (and comparing against a freshly synced base_ctx in smb3_reconfigure()) avoids racing /proc/mounts readers; wide fields (dstaddr, ops/vals) are read under srv_lock and tcon->unix_ext under tc_lock.

Only positive runtime state is reflected: no_linux_ext / nopersistent are not fabricated from inactive runtime state, as a fabricated value committed to cifs_sb->ctx would suppress POSIX / persistent-handle auto-enable on a later reconnect or failover.

This is preparatory plumbing for the next patch, which compares new_ctx against old_ctx to reject non-reconfigurable option changes.

Not addressed (pre-existing): this race is not introduced here. smb3_reconfigure() already replaces cifs_sb->ctx in place with

	smb3_cleanup_fs_context_contents(cifs_sb->ctx);
	memcpy(cifs_sb->ctx, new_ctx, sizeof(*new_ctx));

while cifs_show_options() has always read the same cifs_sb->ctx locklessly for /proc/mounts, so a reader racing a remount of that mount can already observe a torn field or a briefly stale string pointer. The risk is low: remount is an infrequent, admin-triggered operation serialized by s_umount, the only concurrent reader is the diagnostic /proc/mounts path, and the worst outcome is a transiently inconsistent options line for that one mount rather than memory corruption or a crash. This patch neither widens that window nor adds a new lockless reader -- init dup()s cifs_sb->ctx the same way the mount path already does -- so a proper fix (RCU-style ctx replacement) is left as a separate change.

---
 fs/smb/client/cifs_swn.h   |  14 ++-
 fs/smb/client/fs_context.c | 197 ++++++++++++++++++++++++++++++++++++-
 fs/smb/client/smb1ops.c    |   7 +-
 fs/smb/client/smb2pdu.c    |  11 ++-
 4 files changed, 223 insertions(+), 6 deletions(-)
fs/smb/client/cifs_swn.h +12 -2
index 955d07b69450..caf0779d4d07 100644
@@ -26,11 +26,21 @@ void cifs_swn_check(void);
 
 static inline bool cifs_swn_set_server_dstaddr(struct TCP_Server_Info *server)
 {
+	bool ret = false;
+
+	/*
+	 * srv_lock serializes the 128-byte sockaddr_storage write against
+	 * concurrent readers (e.g. cifs_show_address(), reconn_set_ipaddr_
+	 * from_hostname() snapshot, smb3_sync_ctx_from_runtime()) and other
+	 * writers like cifs_chan_update_iface() which already use srv_lock.
+	 */
+	spin_lock(&server->srv_lock);
 	if (server->use_swn_dstaddr) {
 		server->dstaddr = server->swn_dstaddr;
-		return true;
+		ret = true;
 	}
-	return false;
+	spin_unlock(&server->srv_lock);
+	return ret;
 }
 
 static inline void cifs_swn_reset_server_dstaddr(struct TCP_Server_Info *server)
fs/smb/client/fs_context.c +195 -2

228 lines not on this page · raw message

fs/smb/client/smb1ops.c +6 -1
index dc5a8c1da623..16df7c423a9b 100644
@@ -36,11 +36,16 @@ void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
 
 	if (ctx && ctx->no_linux_ext) {
 		tcon->fsUnixInfo.Capability = 0;
+		spin_lock(&tcon->tc_lock);
 		tcon->unix_ext = 0; /* Unix Extensions disabled */
+		spin_unlock(&tcon->tc_lock);
 		cifs_dbg(FYI, "Linux protocol extensions disabled\n");
 		return;
-	} else if (ctx)
+	} else if (ctx) {
+		spin_lock(&tcon->tc_lock);
 		tcon->unix_ext = 1; /* Unix Extensions supported */
+		spin_unlock(&tcon->tc_lock);
+	}
 
 	if (!tcon->unix_ext) {
 		cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
fs/smb/client/smb2pdu.c +10 -1
index 4ce165e40657..ee63b8547728 100644
@@ -1215,8 +1215,10 @@ SMB2_negotiate(const unsigned int xid,
 			goto neg_exit;
 		case SMB311_PROT_ID:
 			/* ops set to 3.0 by default for default so update */
+			spin_lock(&server->srv_lock);
 			server->ops = &smb311_operations;
 			server->vals = &smb311_values;
+			spin_unlock(&server->srv_lock);
 			break;
 		default:
 			break;
@@ -1231,12 +1233,16 @@ SMB2_negotiate(const unsigned int xid,
 			goto neg_exit;
 		case SMB21_PROT_ID:
 			/* ops set to 3.0 by default for default so update */
+			spin_lock(&server->srv_lock);
 			server->ops = &smb21_operations;
 			server->vals = &smb21_values;
+			spin_unlock(&server->srv_lock);
 			break;
 		case SMB311_PROT_ID:
+			spin_lock(&server->srv_lock);
 			server->ops = &smb311_operations;
 			server->vals = &smb311_values;
+			spin_unlock(&server->srv_lock);
 			break;
 		default:
 			break;
@@ -2252,8 +2258,11 @@ SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
 	if (server->ops->validate_negotiate)
 		rc = server->ops->validate_negotiate(xid, tcon);
 	if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
-		if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
+		if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT) {
+			spin_lock(&server->srv_lock);
 			server->nosharesock = true;
+			spin_unlock(&server->srv_lock);
+		}
 tcon_exit:
 
 	free_rsp_buf(resp_buftype, rsp);

base-commit: c0a27675eaf08255017b3cabc28c99c0cd71f468

Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/7 smb: client: block non-reconfigurable option changes on remount

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

Several mount options (seal, sign, vers, ip, rdma, nosharesock, persistent/resilient handles, etc.) require tearing down the connection to take effect, but smb3_verify_reconfigure_ctx() does not reject them. A remount that changes any of these silently ignores the new value, confusing users.

Now that smb3_init_fs_context() duplicates the live context (with runtime state synced in) on reconfigure, simple != checks in smb3_verify_reconfigure_ctx() are sound -- a difference between new_ctx and old_ctx represents a real, intentional change by the user rather than a spurious mismatch against init defaults.

Add comprehensive checks for connection-tied options with clear error messages so the user sees why the remount failed. The server-shared options this rejects (noblocksnd, tcpnodelay, max_credits, min_enc_offload, echo_interval, ...) are compared against a baseline that the previous patch synced from the live server, so a bare remount that merely replays the values already shown in /proc/mounts is accepted; only a genuine change is rejected.

Not addressed here: 'port'. When a share is mounted without an explicit port, old_ctx->port stays 0 even though the connection uses 445, so an explicit 'mount -o remount,port=445' is rejected. This is not reachable through tooling -- port is not printed by cifs_show_options(), so 'mount -a'/libmount never replays it -- and a clean fix collides with the port==0 "use default" sentinel; the only effect is that a user who redundantly re-specifies the active default port on remount gets -EINVAL with a clear message.

---
 fs/smb/client/fs_context.c | 141 +++++++++++++++++++++++++++++++++++++
 1 file changed, 141 insertions(+)
fs/smb/client/fs_context.c +141 -0
index 1a68ae058166..d81bdc875580 100644
@@ -1110,6 +1110,147 @@ static int smb3_verify_reconfigure_ctx(struct fs_context *fc,
 		cifs_errorf(fc, "can not change nbsessinit during remount\n");
 		return -EINVAL;
 	}
+	if (new_ctx->compress != old_ctx->compress) {
+		cifs_errorf(fc, "can not change compress during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->noblocksnd != old_ctx->noblocksnd) {
+		cifs_errorf(fc, "can not change noblocksend during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->noautotune != old_ctx->noautotune) {
+		cifs_errorf(fc, "can not change noautotune during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->no_sparse != old_ctx->no_sparse) {
+		cifs_errorf(fc, "can not change nosparse during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->nodelete != old_ctx->nodelete) {
+		cifs_errorf(fc, "can not change nodelete during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->cruid_specified &&
+	    !uid_eq(new_ctx->cred_uid, old_ctx->cred_uid)) {
+		cifs_errorf(fc, "can not change cruid during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->port != old_ctx->port) {
+		cifs_errorf(fc, "can not change port during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->min_offload != old_ctx->min_offload) {
+		cifs_errorf(fc, "can not change min_enc_offload during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->echo_interval != old_ctx->echo_interval) {
+		cifs_errorf(fc, "can not change echo_interval during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->snapshot_time != old_ctx->snapshot_time) {
+		cifs_errorf(fc, "can not change snapshot during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->max_credits != old_ctx->max_credits) {
+		cifs_errorf(fc, "can not change max_credits during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->handle_timeout != old_ctx->handle_timeout) {
+		cifs_errorf(fc, "can not change handletimeout during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->got_ip &&
+	    !cifs_match_ipaddr((struct sockaddr *)&new_ctx->dstaddr,
+			       (struct sockaddr *)&old_ctx->dstaddr)) {
+		cifs_errorf(fc, "can not change ip during remount\n");
+		return -EINVAL;
+	}
+	if (!cifs_match_ipaddr((struct sockaddr *)&new_ctx->srcaddr,
+			       (struct sockaddr *)&old_ctx->srcaddr)) {
+		cifs_errorf(fc, "can not change srcaddr during remount\n");
+		return -EINVAL;
+	}
+	if (memcmp(new_ctx->source_rfc1001_name, old_ctx->source_rfc1001_name,
+		   RFC1001_NAME_LEN)) {
+		cifs_errorf(fc, "can not change netbiosname during remount\n");
+		return -EINVAL;
+	}
+	if (memcmp(new_ctx->target_rfc1001_name, old_ctx->target_rfc1001_name,
+		   RFC1001_NAME_LEN)) {
+		cifs_errorf(fc, "can not change servern during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->got_version &&
+	    (new_ctx->ops != old_ctx->ops || new_ctx->vals != old_ctx->vals)) {
+		cifs_errorf(fc, "can not change vers during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->witness != old_ctx->witness) {
+		cifs_errorf(fc, "can not change witness during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->rootfs != old_ctx->rootfs) {
+		cifs_errorf(fc, "can not change rootfs during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->linux_ext != old_ctx->linux_ext ||
+	    new_ctx->no_linux_ext != old_ctx->no_linux_ext) {
+		cifs_errorf(fc, "can not change unix during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->nocase != old_ctx->nocase) {
+		cifs_errorf(fc, "can not change nocase during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->intr != old_ctx->intr) {
+		cifs_errorf(fc, "can not change intr during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->no_psx_acl != old_ctx->no_psx_acl) {
+		cifs_errorf(fc, "can not change acl during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->local_lease != old_ctx->local_lease) {
+		cifs_errorf(fc, "can not change locallease during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->sign != old_ctx->sign) {
+		cifs_errorf(fc, "can not change sign during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->ignore_signature != old_ctx->ignore_signature) {
+		cifs_errorf(fc, "can not change ignore_signature (signloosely) during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->seal != old_ctx->seal) {
+		cifs_errorf(fc, "can not change seal during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->nosharesock != old_ctx->nosharesock) {
+		cifs_errorf(fc, "can not change nosharesock during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->persistent != old_ctx->persistent ||
+	    new_ctx->nopersistent != old_ctx->nopersistent) {
+		cifs_errorf(fc, "can not change persistenthandles during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->resilient != old_ctx->resilient) {
+		cifs_errorf(fc, "can not change resilienthandles during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->sockopt_tcp_nodelay != old_ctx->sockopt_tcp_nodelay) {
+		cifs_errorf(fc, "can not change tcpnodelay during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->domainauto != old_ctx->domainauto) {
+		cifs_errorf(fc, "can not change domainauto during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->rdma != old_ctx->rdma) {
+		cifs_errorf(fc, "can not change rdma during remount\n");
+		return -EINVAL;
+	}
 
 	return 0;
 }
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/7 smb: client: sync retrans on remount

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

The retrans mount option controls how many times the client retries a request before giving up. Remount stored the new value in cifs_sb->ctx but never pushed it to server->retrans, so the live connection kept using the old count.

Add smb3_sync_server_opts() to copy ctx options that live on TCP_Server_Info into the running server after a successful remount. For now it handles retrans; later patches extend it to other server-level knobs. Because the earlier "sync runtime state into ctx" patch seeds the baseline retrans from the live server, a bare 'mount -o remount' carries the current value forward and the parser only overwrites it when the user passes retrans=N -- so an explicit retrans=0 now correctly resets to the compile-time default.

server->retrans is a plain word shared by all mounts on the connection, so reconfiguring one mount changes the value seen by the others. That matches existing semantics: cifs_show_options() already reports server->retrans per mount and the retry path always uses the server's value. Its access sites are each a single independent read or write, so they use READ_ONCE()/WRITE_ONCE() to avoid tearing rather than taking srv_lock.

---
 fs/smb/client/cifsfs.c     |  6 ++++--
 fs/smb/client/connect.c    |  4 ++--
 fs/smb/client/fs_context.c | 30 ++++++++++++++++++++++++++++++
 fs/smb/client/smb2ops.c    |  2 +-
 4 files changed, 37 insertions(+), 5 deletions(-)
fs/smb/client/cifsfs.c +4 -2
index a1dacc7d8f74..07d58b26f455 100644
@@ -627,6 +627,7 @@ cifs_show_options(struct seq_file *s, struct dentry *root)
 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 	struct sockaddr *srcaddr;
 	unsigned int sbflags;
+	unsigned int retrans;
 
 	srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
 
@@ -782,8 +783,9 @@ cifs_show_options(struct seq_file *s, struct dentry *root)
 		seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize);
 	if (tcon->ses->server->min_offload)
 		seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
-	if (tcon->ses->server->retrans)
-		seq_printf(s, ",retrans=%u", tcon->ses->server->retrans);
+	retrans = READ_ONCE(tcon->ses->server->retrans);
+	if (retrans)
+		seq_printf(s, ",retrans=%u", retrans);
 	seq_printf(s, ",echo_interval=%lu",
 			tcon->ses->server->echo_interval / HZ);
 
fs/smb/client/connect.c +2 -2
index ba749ec25a59..b5d257a83b4f 100644
@@ -1658,7 +1658,7 @@ static int match_server(struct TCP_Server_Info *server,
 	if (server->min_offload != ctx->min_offload)
 		return 0;
 
-	if (server->retrans != ctx->retrans)
+	if (READ_ONCE(server->retrans) != ctx->retrans)
 		return 0;
 
 	return 1;
@@ -1873,7 +1873,7 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
 	 */
 	__module_get(THIS_MODULE);
 	tcp_ses->min_offload = ctx->min_offload;
-	tcp_ses->retrans = ctx->retrans;
+	WRITE_ONCE(tcp_ses->retrans, ctx->retrans);
 	/*
 	 * at this point we are the only ones with the pointer
 	 * to the struct since the kernel thread not created yet
fs/smb/client/fs_context.c +30 -0
index d81bdc875580..2fb40e9e1652 100644
@@ -964,6 +964,16 @@ static int smb3_sync_ctx_from_runtime(struct cifs_sb_info *cifs_sb,
 	ctx->nosharesock = server->nosharesock;
 	spin_unlock(&server->srv_lock);
 
+	/*
+	 * retrans lives on the shared TCP_Server_Info and can be updated by
+	 * a remount of any mount sharing the connection.  Sync it from the
+	 * live server so a bare remount carries the current value forward
+	 * instead of writing a stale per-mount copy back in
+	 * smb3_sync_server_opts().  Read with READ_ONCE to pair with the
+	 * lockless WRITE_ONCE writers and the runtime retry-path reader.
+	 */
+	ctx->retrans = READ_ONCE(server->retrans);
+
 	/*
 	 * These connection-tied options live on the shared TCP_Server_Info and
 	 * are reported by cifs_show_options() from server->*, not from the
@@ -1318,6 +1328,24 @@ static void smb3_sync_ses_chan_max(struct cifs_ses *ses, size_t max_channels)
 	spin_unlock(&ses->chan_lock);
 }
 
+/*
+ * Synchronize server-level options that are stored on TCP_Server_Info
+ * at mount time.  These fields are consulted at runtime (retry logic)
+ * so remount needs to update the live server struct in addition to
+ * cifs_sb->ctx.  Note these live on TCP_Server_Info and are therefore
+ * shared across all mounts to the same server; reconfiguring one mount
+ * updates the value seen by every other mount sharing the connection,
+ * matching how cifs_show_options() and the runtime retry path already
+ * read them unsynchronized from the server struct.
+ */
+static void smb3_sync_server_opts(struct cifs_sb_info *cifs_sb)
+{
+	struct TCP_Server_Info *server = cifs_sb_master_tcon(cifs_sb)->ses->server;
+	struct smb3_fs_context *ctx = cifs_sb->ctx;
+
+	WRITE_ONCE(server->retrans, ctx->retrans);
+}
+
 static int smb3_reconfigure(struct fs_context *fc)
 {
 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
@@ -1516,6 +1544,8 @@ static int smb3_reconfigure(struct fs_context *fc)
 	if (!rc)
 		rc = dfs_cache_remount_fs(cifs_sb);
 #endif
+	if (!rc)
+		smb3_sync_server_opts(cifs_sb);
 
 	return rc;
 
fs/smb/client/smb2ops.c +1 -1
index 192649fec25d..0debeb36447d 100644
@@ -2837,7 +2837,7 @@ bool smb2_should_replay(struct cifs_tcon *tcon,
 	if (!pretries || !pcur_sleep)
 		return false;
 
-	if (tcon->retry || (*pretries)++ < tcon->ses->server->retrans) {
+	if (tcon->retry || (*pretries)++ < READ_ONCE(tcon->ses->server->retrans)) {
 		/* Update sleep time for exponential backoff */
 		if (!(*pcur_sleep))
 			(*pcur_sleep) = 1;
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/7 smb: client: block cache=ro and cache=singleclient on remount

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

cache=ro and cache=singleclient are mount-time environment declarations where the admin promises that the share is read-only or exclusively accessed. The client bypasses server-based coherency (oplocks/leases) and caches aggressively based on this promise.

These modes were intentionally excluded from smb3_update_mnt_flags() when it was introduced in commit 2d39f50c2b15 ("cifs: move update of flags into a separate function") — only cache=strict, cache=none and cache=loose were made reconfigurable. However, remount currently silently accepts cache=ro and cache=singleclient without actually applying them, which is confusing.

Add explicit checks in smb3_verify_reconfigure_ctx() to reject attempts to change these options during remount with a clear error message.

---
 fs/smb/client/fs_context.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
fs/smb/client/fs_context.c +13 -0
index 2fb40e9e1652..4a319c113196 100644
@@ -1261,6 +1261,19 @@ static int smb3_verify_reconfigure_ctx(struct fs_context *fc,
 		cifs_errorf(fc, "can not change rdma during remount\n");
 		return -EINVAL;
 	}
+	/*
+	 * cache=ro sets cache_ro and cache=singleclient sets cache_rw; the
+	 * other cache modes (strict/loose/none) leave both clear and are
+	 * handled separately in smb3_update_mnt_flags().
+	 */
+	if (new_ctx->cache_ro != old_ctx->cache_ro) {
+		cifs_errorf(fc, "can not change cache=ro during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->cache_rw != old_ctx->cache_rw) {
+		cifs_errorf(fc, "can not change cache=singleclient during remount\n");
+		return -EINVAL;
+	}
 
 	return 0;
 }
Signature · 1 line
2.43.0
#5 lore raw

PATCH 5/7 smb: client: apply rasize on remount

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

rasize is accepted during remount parsing but sb->s_bdi->ra_pages is only set in cifs_read_super() at mount time. A remount with a new rasize value silently has no effect on the readahead window.

Update ra_pages in smb3_reconfigure() after the context has been duplicated, using the same logic as cifs_read_super(): if rasize is set, use it directly; otherwise fall back to 2 * rsize.

---
 fs/smb/client/fs_context.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/smb/client/fs_context.c +9 -2
index 4a319c113196..ee8e38c3b7f4 100644
@@ -1368,7 +1368,7 @@ static int smb3_reconfigure(struct fs_context *fc)
 	struct dentry *root = fc->root;
 	struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
 	struct cifs_ses *ses = cifs_sb_master_tcon(cifs_sb)->ses;
-	unsigned int rsize = ctx->rsize, wsize = ctx->wsize;
+	unsigned int rsize = ctx->rsize, wsize = ctx->wsize, rasize = ctx->rasize;
 	char *new_password = NULL, *new_password2 = NULL;
 	bool need_recon = false;
 	bool need_mchan_update;
@@ -1462,9 +1462,10 @@ static int smb3_reconfigure(struct fs_context *fc)
 		STEAL_STRING_SENSITIVE(cifs_sb, ctx, password2);
 	}
 
-	/* if rsize or wsize not passed in on remount, use previous values */
+	/* if rsize, wsize, or rasize not passed in on remount, use previous values */
 	ctx->rsize = rsize ? CIFS_ALIGN_RSIZE(fc, rsize) : cifs_sb->ctx->rsize;
 	ctx->wsize = wsize ? CIFS_ALIGN_WSIZE(fc, wsize) : cifs_sb->ctx->wsize;
+	ctx->rasize = rasize ? rasize : cifs_sb->ctx->rasize;
 
 	new_ctx = kzalloc_obj(*new_ctx);
 	if (!new_ctx) {
@@ -1553,6 +1554,12 @@ static int smb3_reconfigure(struct fs_context *fc)
 	smb3_cleanup_fs_context(old_ctx);
 	old_ctx = NULL;
 	smb3_update_mnt_flags(cifs_sb);
+
+	if (cifs_sb->ctx->rasize)
+		root->d_sb->s_bdi->ra_pages = cifs_sb->ctx->rasize / PAGE_SIZE;
+	else
+		root->d_sb->s_bdi->ra_pages = 2 * (cifs_sb->ctx->rsize / PAGE_SIZE);
+
 #ifdef CONFIG_CIFS_DFS_UPCALL
 	if (!rc)
 		rc = dfs_cache_remount_fs(cifs_sb);
Signature · 1 line
2.43.0
#6 lore raw

PATCH 6/7 smb: client: move struct tcon_list to cifsglob.h

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

struct tcon_list is a small list-node wrapper that pairs a list_head with a cifs_tcon pointer, used to safely iterate tcons under a superblock outside tlink_tree_lock.

It is currently file-static in misc.c, used only by cifs_close_all_deferred_files_sb(). Move it next to the similar struct file_list in cifsglob.h so that other _sb() iteration helpers can reuse it instead of redefining the same shape locally.

No functional change.

---
 fs/smb/client/cifsglob.h | 5 +++++
 fs/smb/client/misc.c     | 5 -----
 2 files changed, 5 insertions(+), 5 deletions(-)
fs/smb/client/cifsglob.h +5 -0
index 79e4e84f8985..3bd2a588e7c4 100644
@@ -1812,6 +1812,11 @@ struct file_list {
 	struct cifsFileInfo *cfile;
 };
 
+struct tcon_list {
+	struct list_head entry;
+	struct cifs_tcon *tcon;
+};
+
 struct cifs_mount_ctx {
 	struct cifs_sb_info *cifs_sb;
 	struct smb3_fs_context *fs_ctx;
fs/smb/client/misc.c +0 -5
index 46e1382e8e04..7fb38f92cd5a 100644
@@ -28,11 +28,6 @@
 #include "fs_context.h"
 #include "cached_dir.h"
 
-struct tcon_list {
-	struct list_head entry;
-	struct cifs_tcon *tcon;
-};
-
 /* The xid serves as a useful identifier for each incoming vfs request,
    in a similar way to the mid which is useful to track each sent smb,
    and CurrentXid can also provide a running counter (although it
Signature · 1 line
2.43.0
#7 lore raw

PATCH 7/7 smb: client: allow nolease option to be reconfigured on remount

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

Changing nolease via remount is silently accepted but has no effect:
the value is never propagated to the live tcons, and state from before the switch keeps using the old behavior.

Make nolease take effect on remount:

- Add smb3_sync_tcon_opts(), called from smb3_reconfigure() after the context is updated, to propagate ctx->no_lease to every tcon under the superblock (walking tlink_tree under tlink_tree_lock) so future opens honor the new setting.

- On switch to nolease, drop deferred file handles via cifs_close_all_deferred_files_sb() and evict cached directory fids via the new invalidate_all_cached_dirs_sb(), since each holds a lease.

invalidate_all_cached_dirs() gains a close_handles argument: the live remount passes true so smb2_close_cached_fid() actually sends SMB2_close to release the server-side directory handle and lease; the session-loss/teardown callers pass false, keeping their old behavior. invalidate_all_cached_dirs_sb() counts the tcons under the lock, allocates the pointer array with GFP_KERNEL outside it, then re-takes the lock to collect a reference on each tcon and invalidates outside the lock (it can sleep).

Move tcon->no_lease out of the bitfield into a plain bool: as a runtime writer it would otherwise share a word with need_reconnect/ need_reopen_files, and a bitfield read-modify-write could clobber a concurrent lockless update of those.

Existing open handles keep their leases until the server breaks them or userspace closes the file -- nolease only governs new opens.

Not addressed here (pre-existing, unrelated to nolease):
cached_dir_lease_break() updates tcon->tc_count without tc_lock; cfids_laundromat_worker() can resurrect a tcon from tc_count 0 without a TID_EXITING check, racing cifs_put_tcon(); and the cifs_debug dirs knob still invalidates with close_handles=false. These are left for separate changes.

---
 fs/smb/client/cached_dir.c | 83 ++++++++++++++++++++++++++++++++++----
 fs/smb/client/cached_dir.h |  4 +-
 fs/smb/client/cifs_debug.c |  2 +-
 fs/smb/client/cifsglob.h   |  2 +-
 fs/smb/client/file.c       |  2 +-
 fs/smb/client/fs_context.c | 54 +++++++++++++++++++++++++
 fs/smb/client/smb2pdu.c    |  2 +-
 fs/smb/client/trace.h      |  2 +
 8 files changed, 139 insertions(+), 12 deletions(-)
fs/smb/client/cached_dir.c +76 -7
index 88d5e9a32f28..3968e4f074a8 100644
@@ -598,10 +598,17 @@ void close_all_cached_dirs(struct cifs_sb_info *cifs_sb)
 }
 
 /*
- * Invalidate all cached dirs when a TCON has been reset
- * due to a session loss.
+ * Invalidate all cached dirs on a TCON, moving them to the dying list
+ * for the laundromat to clean up.
+ *
+ * @close_handles: if true, the connection is still live (e.g. remount),
+ * so leave cfid->is_open set and let smb2_close_cached_fid() send
+ * SMB2_close to release the server-side directory handle and lease.  If
+ * false (the session-loss teardown case), mark the cfids closed so the
+ * doomed SMB2_close is skipped -- the server has already dropped them.
  */
-void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync)
+void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync,
+				bool close_handles)
 {
 	struct cached_fids *cfids = tcon->cfids;
 	struct cached_fid *cfid, *q;
@@ -610,15 +617,16 @@ void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync)
 		return;
 
 	/*
-	 * Mark all the cfids as closed, and move them to the cfids->dying list.
-	 * They'll be cleaned up by laundromat.  Take a reference to each cfid
-	 * during this process.
+	 * Move all the cfids to the cfids->dying list.  They'll be cleaned
+	 * up by laundromat.  Take a reference to each cfid during this
+	 * process.
 	 */
 	spin_lock(&cfids->cfid_list_lock);
 	list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
 		list_move(&cfid->entry, &cfids->dying);
 		cfids->num_entries--;
-		cfid->is_open = false;
+		if (!close_handles)
+			cfid->is_open = false;
 		cfid->on_list = false;
 		if (cfid->has_lease) {
 			/*
@@ -637,6 +645,67 @@ void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync)
 		flush_delayed_work(&cfids->laundromat_work);
 }
 
+/*
+ * Invalidate cached directory entries across all tcons under a
+ * superblock.  Count the tcons first so the pointer array can be
+ * allocated with GFP_KERNEL outside tlink_tree_lock: allocating
+ * per-tcon under the spinlock would force GFP_ATOMIC and, on failure,
+ * silently leave some tcons with stale leases after a nolease remount.
+ * References are then taken under tlink_tree_lock and the cached dirs
+ * closed outside the spinlock since that can sleep.  Holding a tc_count
+ * reference prevents the tcon from being freed by tlink_expire_delayed()
+ * between dropping the spinlock and the call.
+ *
+ * Called on remount while the connection is live (e.g. switching to
+ * nolease), so pass close_handles=true to actually release the
+ * server-side directory handles and their leases.
+ */
+void invalidate_all_cached_dirs_sb(struct cifs_sb_info *cifs_sb)
+{
+	struct rb_root *root = &cifs_sb->tlink_tree;
+	struct rb_node *node;
+	struct cifs_tcon *tcon;
+	struct tcon_link *tlink;
+	struct cifs_tcon **tcons;
+	unsigned int i, n = 0, count = 0;
+
+	spin_lock(&cifs_sb->tlink_tree_lock);
+	for (node = rb_first(root); node; node = rb_next(node)) {
+		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
+		if (!IS_ERR(tlink_tcon(tlink)))
+			count++;
+	}
+	spin_unlock(&cifs_sb->tlink_tree_lock);
+
+	if (!count)
+		return;
+
+	tcons = kcalloc(count, sizeof(*tcons), GFP_KERNEL);
+	if (!tcons)
+		return;
+
+	spin_lock(&cifs_sb->tlink_tree_lock);
+	for (node = rb_first(root); node && n < count; node = rb_next(node)) {
+		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
+		tcon = tlink_tcon(tlink);
+		if (IS_ERR(tcon))
+			continue;
+		spin_lock(&tcon->tc_lock);
+		++tcon->tc_count;
+		trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
+				    netfs_trace_tcon_ref_get_cached_inval_sb);
+		spin_unlock(&tcon->tc_lock);
+		tcons[n++] = tcon;
+	}
+	spin_unlock(&cifs_sb->tlink_tree_lock);
+
+	for (i = 0; i < n; i++) {
+		invalidate_all_cached_dirs(tcons[i], true, true);
+		cifs_put_tcon(tcons[i], netfs_trace_tcon_ref_put_cached_inval_sb);
+	}
+	kfree(tcons);
+}
+
 static void
 cached_dir_offload_close(struct work_struct *work)
 {
fs/smb/client/cached_dir.h +3 -1
index fc756836da95..3b86339ca884 100644
@@ -90,7 +90,9 @@ void close_cached_dir(struct cached_fid *cfid);
 void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon,
 			     const char *name, struct cifs_sb_info *cifs_sb);
 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb);
-void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync);
+void invalidate_all_cached_dirs_sb(struct cifs_sb_info *cifs_sb);
+void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync,
+				bool close_handles);
 bool cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16]);
 
 #endif			/* _CACHED_DIR_H */
fs/smb/client/cifs_debug.c +1 -1
index 4ed4f55a0bb7..28b0d433810f 100644
@@ -383,7 +383,7 @@ static ssize_t cifs_debug_dirs_proc_write(struct file *file, const char __user *
 				if (cifs_ses_exiting(ses))
 					continue;
 				list_for_each_entry(tcon, &ses->tcon_list, tcon_list)
-					invalidate_all_cached_dirs(tcon, false);
+					invalidate_all_cached_dirs(tcon, false, false);
 			}
 		}
 		spin_unlock(&cifs_tcp_ses_lock);
fs/smb/client/cifsglob.h +1 -1
index 3bd2a588e7c4..2c5bc4dee5b2 100644
@@ -1265,9 +1265,9 @@ struct cifs_tcon {
 	bool need_reopen_files:1; /* need to reopen tcon file handles */
 	bool use_resilient:1; /* use resilient instead of durable handles */
 	bool use_persistent:1; /* use persistent instead of durable handles */
-	bool no_lease:1;    /* Do not request leases on files or directories */
 	bool use_witness:1; /* use witness protocol */
 	bool dummy:1; /* dummy tcon used for reconnecting channels */
+	bool no_lease;    /* Do not request leases on files or directories */
 	__le32 capabilities;
 	__u32 share_flags;
 	__u32 maximal_access;
fs/smb/client/file.c +1 -1
index ac89c1ba56b1..852687916c4f 100644
@@ -399,7 +399,7 @@ cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
 	}
 	spin_unlock(&tcon->open_file_lock);
 
-	invalidate_all_cached_dirs(tcon, true);
+	invalidate_all_cached_dirs(tcon, true, false);
 	spin_lock(&tcon->tc_lock);
 	if (tcon->status == TID_IN_FILES_INVALIDATE)
 		tcon->status = TID_NEED_TCON;
fs/smb/client/fs_context.c +54 -0

72 lines not on this page · raw message

fs/smb/client/smb2pdu.c +1 -1
index ee63b8547728..baef7efd78c7 100644
@@ -2304,7 +2304,7 @@ SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
 	}
 	spin_unlock(&ses->chan_lock);
 
-	invalidate_all_cached_dirs(tcon, true);
+	invalidate_all_cached_dirs(tcon, true, false);
 
 	rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, server,
 				 (void **) &req,
fs/smb/client/trace.h +2 -0
index 5b21ad3c15fb..1e61a36759ed 100644
@@ -173,6 +173,7 @@
 	EM(netfs_trace_tcon_ref_free_ipc,		"FRE Ipc   ") \
 	EM(netfs_trace_tcon_ref_free_ipc_fail,		"FRE Ipc-F ") \
 	EM(netfs_trace_tcon_ref_free_reconnect_server,	"FRE Reconn") \
+	EM(netfs_trace_tcon_ref_get_cached_inval_sb,	"GET Ch-IvS") \
 	EM(netfs_trace_tcon_ref_get_cached_laundromat,	"GET Ch-Lau") \
 	EM(netfs_trace_tcon_ref_get_cached_lease_break,	"GET Ch-Lea") \
 	EM(netfs_trace_tcon_ref_get_cancelled_close,	"GET Cn-Cls") \
@@ -186,6 +187,7 @@
 	EM(netfs_trace_tcon_ref_new_ipc,		"NEW Ipc   ") \
 	EM(netfs_trace_tcon_ref_new_reconnect_server,	"NEW Reconn") \
 	EM(netfs_trace_tcon_ref_put_cached_close,	"PUT Ch-Cls") \
+	EM(netfs_trace_tcon_ref_put_cached_inval_sb,	"PUT Ch-IvS") \
 	EM(netfs_trace_tcon_ref_put_cancelled_close,	"PUT Cn-Cls") \
 	EM(netfs_trace_tcon_ref_put_cancelled_close_fid, "PUT Cn-Fid") \
 	EM(netfs_trace_tcon_ref_put_cancelled_mid,	"PUT Cn-Mid") \
Signature · 1 line
2.43.0
#8 lore raw

v2 cover smb: client: remount reconfigure option fixes

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

This series makes several mount options actually take effect (or be cleanly rejected) on remount instead of being silently ignored, and fixes the option-comparison baseline so a bare 'mount -o remount' is neither wrongly rejected nor allowed to clobber live runtime state.

Overview:
1: build the reconfigure baseline from the live mount (a private,

     runtime-synced dup of cifs_sb->ctx) instead of init defaults.

2: reject connection-tied options that cannot change on remount,
using that baseline so bare remounts and /proc/mounts replays are still accepted.
3: propagate retrans to the live server on remount.
4: reject cache=ro / cache=singleclient changes on remount. 5: apply rasize on remount.
6: move struct tcon_list to cifsglob.h (no functional change). 7: make nolease take effect on remount and evict lease-bearing
cached state.
8: take tc_lock around the tc_count bump in cached_dir_lease_break().

Changes since v1:
- Patch 1: corrected the "Not addressed" note. The pre-existing unserialized cifs_sb->ctx access is a potential use-after-free read of freed string fields (e.g. iocharset), not merely a torn or stale value as v1 claimed; the note now also acknowledges that the reconfigure-time dup adds one more reader on that same footing. No code change.
- Patch 3: cifs_construct_tcon() (sess.c) now reads server->retrans with READ_ONCE(); it was the one remaining plain read that would race the new WRITE_ONCE() writer. (Reported by automated review.)
- Patch 5: documented in the commit message the two pre-existing items previously noted only here (sub-PAGE_SIZE rsize/wsize realign, and the STEAL_STRING() source NULL window vs DFS reconnect).
- Patch 7: invalidate_all_cached_dirs_sb() is now documented as best-effort if the tcon-array allocation fails under memory pressure -- future opens still honor the new nolease via the tcon->no_lease propagation, and the n<count walk bound is commented. (Reported by automated review.)
- New Patch 8: take tcon->tc_lock around the tc_count bump in cached_dir_lease_break() -- the sole tc_count writer that lacked it, letting a lease break race another put/get. (Reported by automated review.)

Known pre-existing issues intentionally NOT addressed here (each is called out in the relevant commit message):
- cifs_sb->ctx has no lifetime protection: cifs_show_options() and other lockless readers can take a use-after-free read of string fields while smb3_reconfigure() frees/replaces them in place. The reader side (/proc/mounts) is unprivileged but a hit still needs a concurrent privileged remount of that same mount. The correct fix is an RCU/refcount ctx lifetime touching every reader (iocharset, source/devname, the reconfigure dup, DFS source lookup, ...) and is best done as its own change. (Patch 1, Patch 5)
- tcon->unix_ext shares a bitfield word with other lockless writers; theoretical word-tearing only. (Patch 1)
- Explicit 'mount -o remount,port=<active-default>' is rejected; not reachable via tooling since port is not in /proc/mounts. (Patch 2)
- A bare remount realigns a sub-PAGE_SIZE negotiated rsize/wsize up to PAGE_SIZE via CIFS_ALIGN_*; pre-existing and independent of the rasize change here. (Patch 5)
- cfids_laundromat_worker() can resurrect a tcon from tc_count 0 while cifs_put_tcon() tears it down, and the cifs_debug dirs knob invalidates with close_handles=false. Pre-existing and unrelated to nolease. (Patch 7)

Testing:
- checkpatch --strict clean (bar one pre-existing CamelCase CHECK on

    ses->domainName); each patch builds standalone (bisectable).

- Functional testing on Azure Files (SMB 3.1.1): a bare remount preserves the full /proc/mounts option set; retrans/nolease/rasize apply on remount; vers / echo_interval and other non-reconfigurable changes are rejected with -EINVAL; a remount replaying a server-displayed option is accepted; I/O works and dmesg is clean.

Rajasi Mandal (8):
smb: client: sync runtime state into ctx on reconfigure
smb: client: block non-reconfigurable option changes on remount
smb: client: sync retrans on remount
smb: client: block cache=ro and cache=singleclient on remount
smb: client: apply rasize on remount
smb: client: move struct tcon_list to cifsglob.h
smb: client: allow nolease option to be reconfigured on remount
smb: client: take tc_lock around tc_count bump in
cached_dir_lease_break()

 fs/smb/client/cached_dir.c |  91 +++++++-
 fs/smb/client/cached_dir.h |   4 +-
 fs/smb/client/cifs_debug.c |   2 +-
 fs/smb/client/cifs_swn.h   |  14 +-
 fs/smb/client/cifsfs.c     |   6 +-
 fs/smb/client/cifsglob.h   |   7 +-
 fs/smb/client/connect.c    |   4 +-
 fs/smb/client/file.c       |   2 +-
 fs/smb/client/fs_context.c | 446 ++++++++++++++++++++++++++++++++++++-
 fs/smb/client/misc.c       |   5 -
 fs/smb/client/sess.c       |   2 +-
 fs/smb/client/smb1ops.c    |   7 +-
 fs/smb/client/smb2ops.c    |   2 +-
 fs/smb/client/smb2pdu.c    |  13 +-
 fs/smb/client/trace.h      |   2 +
 15 files changed, 576 insertions(+), 31 deletions(-)

base-commit: c0a27675eaf08255017b3cabc28c99c0cd71f468

Signature · 1 line
2.43.0
#9 lore raw

PATCH v2 1/8 smb: client: sync runtime state into ctx on reconfigure

From: Rajasi Mandal <[email protected]>

smb3_init_fs_context() builds a fresh context from init defaults on every call, including remount. Many options shown by cifs_show_options() come from tcon/server/ses rather than ctx, so those defaults are a poor baseline for deciding what changed on remount.

On reconfigure, duplicate the live cifs_sb->ctx into a private context and sync it with runtime state via a new smb3_sync_ctx_from_runtime() so the baseline matches /proc/mounts. This mirrors libmount (util-linux), which replays the current /proc/mounts options on
remount: unless the baseline equals the displayed state, a bare 'mount -o remount' looks like a change and is rejected. Only positive runtime state is reflected -- no_linux_ext / nopersistent are not fabricated, as a fabricated value would suppress POSIX / persistent- handle auto-enable on a later reconnect.

This is preparatory plumbing for the next patch, which compares new_ctx against old_ctx to reject non-reconfigurable option changes.

Not addressed (pre-existing): cifs_sb->ctx has no lifetime protection. cifs_show_options() reads string fields (e.g. ctx->iocharset) locklessly while smb3_reconfigure() frees and replaces them in place, so a reader racing a remount can take a use-after-free read (KASAN splat/oops), not merely a stale value. The reconfigure-time dup added here is one such lockless reader. The exposure is bounded -- reconfigure is privileged and serialized by s_umount -- and the proper fix (an RCU/refcount ctx lifetime) is a larger, standalone change.

---
 fs/smb/client/cifs_swn.h   |  14 ++-
 fs/smb/client/fs_context.c | 197 ++++++++++++++++++++++++++++++++++++-
 fs/smb/client/smb1ops.c    |   7 +-
 fs/smb/client/smb2pdu.c    |  11 ++-
 4 files changed, 223 insertions(+), 6 deletions(-)
fs/smb/client/cifs_swn.h +12 -2
index 955d07b69450..caf0779d4d07 100644
@@ -26,11 +26,21 @@ void cifs_swn_check(void);
 
 static inline bool cifs_swn_set_server_dstaddr(struct TCP_Server_Info *server)
 {
+	bool ret = false;
+
+	/*
+	 * srv_lock serializes the 128-byte sockaddr_storage write against
+	 * concurrent readers (e.g. cifs_show_address(), reconn_set_ipaddr_
+	 * from_hostname() snapshot, smb3_sync_ctx_from_runtime()) and other
+	 * writers like cifs_chan_update_iface() which already use srv_lock.
+	 */
+	spin_lock(&server->srv_lock);
 	if (server->use_swn_dstaddr) {
 		server->dstaddr = server->swn_dstaddr;
-		return true;
+		ret = true;
 	}
-	return false;
+	spin_unlock(&server->srv_lock);
+	return ret;
 }
 
 static inline void cifs_swn_reset_server_dstaddr(struct TCP_Server_Info *server)
fs/smb/client/fs_context.c +195 -2

228 lines not on this page · raw message

fs/smb/client/smb1ops.c +6 -1
index dc5a8c1da623..16df7c423a9b 100644
@@ -36,11 +36,16 @@ void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
 
 	if (ctx && ctx->no_linux_ext) {
 		tcon->fsUnixInfo.Capability = 0;
+		spin_lock(&tcon->tc_lock);
 		tcon->unix_ext = 0; /* Unix Extensions disabled */
+		spin_unlock(&tcon->tc_lock);
 		cifs_dbg(FYI, "Linux protocol extensions disabled\n");
 		return;
-	} else if (ctx)
+	} else if (ctx) {
+		spin_lock(&tcon->tc_lock);
 		tcon->unix_ext = 1; /* Unix Extensions supported */
+		spin_unlock(&tcon->tc_lock);
+	}
 
 	if (!tcon->unix_ext) {
 		cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
fs/smb/client/smb2pdu.c +10 -1
index 4ce165e40657..ee63b8547728 100644
@@ -1215,8 +1215,10 @@ SMB2_negotiate(const unsigned int xid,
 			goto neg_exit;
 		case SMB311_PROT_ID:
 			/* ops set to 3.0 by default for default so update */
+			spin_lock(&server->srv_lock);
 			server->ops = &smb311_operations;
 			server->vals = &smb311_values;
+			spin_unlock(&server->srv_lock);
 			break;
 		default:
 			break;
@@ -1231,12 +1233,16 @@ SMB2_negotiate(const unsigned int xid,
 			goto neg_exit;
 		case SMB21_PROT_ID:
 			/* ops set to 3.0 by default for default so update */
+			spin_lock(&server->srv_lock);
 			server->ops = &smb21_operations;
 			server->vals = &smb21_values;
+			spin_unlock(&server->srv_lock);
 			break;
 		case SMB311_PROT_ID:
+			spin_lock(&server->srv_lock);
 			server->ops = &smb311_operations;
 			server->vals = &smb311_values;
+			spin_unlock(&server->srv_lock);
 			break;
 		default:
 			break;
@@ -2252,8 +2258,11 @@ SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
 	if (server->ops->validate_negotiate)
 		rc = server->ops->validate_negotiate(xid, tcon);
 	if (rc == 0) /* See MS-SMB2 2.2.10 and 3.2.5.5 */
-		if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT)
+		if (tcon->share_flags & SMB2_SHAREFLAG_ISOLATED_TRANSPORT) {
+			spin_lock(&server->srv_lock);
 			server->nosharesock = true;
+			spin_unlock(&server->srv_lock);
+		}
 tcon_exit:
 
 	free_rsp_buf(resp_buftype, rsp);

base-commit: c0a27675eaf08255017b3cabc28c99c0cd71f468

Signature · 1 line
2.43.0
maintainer
#10 lore raw

Re: [PATCH v2 1/8] smb: client: sync runtime state into ctx on reconfigure

Quoted 18 lines
fs/smb/client/fs_context.c +14 -0
@@ -1961,6 +2112,48 @@ int smb3_init_fs_context(struct fs_context *fc)
        char *nodename = utsname()->nodename;
        int i;
 
+       /*
+        * For reconfigure (remount), duplicate the existing mount context
+        * instead of building one from scratch with init defaults.
+        *
+        * VFS sets fc->root before calling init_fs_context for reconfigure,
+        * so we can access the existing superblock's context.  We dup the
+        * live cifs_sb->ctx into a private new_ctx, then sync new_ctx with
+        * runtime state (tcon/server/ses) so it matches what
+        * cifs_show_options() displays.  Syncing into the private copy (not
+        * the shared cifs_sb->ctx) avoids racing with concurrent
+        * fspick/remount and /proc/mounts readers.  The parser will overwrite
+        * only the options explicitly passed on remount, so any difference
+        * between new_ctx and old_ctx in smb3_verify_reconfigure_ctx()
+        * represents a real, intentional change by the user.

fs/smb/client/fs_context.c int smb3_init_fs_context(struct fs_context *fc) · line 2138

Quoted 10 lines
fs/smb/client/fs_context.c +10 -0
+        */
+       if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
+               struct cifs_sb_info *cifs_sb = CIFS_SB(fc->root->d_sb);
+               int rc;
+
+               ctx = kzalloc_obj(struct smb3_fs_context);
+               if (!ctx)
+                       return -ENOMEM;
+
+               rc = smb3_fs_context_dup(ctx, cifs_sb->ctx);

Doesn't this add another potential UAF path? smb3_init_fs_context() may run before s_umount is acquired, so this duplication could race with another remount freeing or replacing strings in cifs_sb->ctx.

#11 lore raw

PATCH v2 2/8 smb: client: block non-reconfigurable option changes on remount

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

Several mount options (seal, sign, vers, ip, rdma, nosharesock, persistent/resilient handles, etc.) require tearing down the connection to take effect, but smb3_verify_reconfigure_ctx() does not reject them. A remount that changes any of these silently ignores the new value, confusing users.

Now that smb3_init_fs_context() duplicates the live context (with runtime state synced in) on reconfigure, simple != checks in smb3_verify_reconfigure_ctx() are sound -- a difference between new_ctx and old_ctx represents a real, intentional change by the user rather than a spurious mismatch against init defaults.

Add comprehensive checks for connection-tied options with clear error messages so the user sees why the remount failed. The server-shared options this rejects (noblocksnd, tcpnodelay, max_credits, min_enc_offload, echo_interval, ...) are compared against a baseline that the previous patch synced from the live server, so a bare remount that merely replays the values already shown in /proc/mounts is accepted; only a genuine change is rejected.

Not addressed here: 'port'. When a share is mounted without an explicit port, old_ctx->port stays 0 even though the connection uses 445, so an explicit 'mount -o remount,port=445' is rejected. This is not reachable through tooling -- port is not printed by cifs_show_options(), so 'mount -a'/libmount never replays it -- and a clean fix collides with the port==0 "use default" sentinel; the only effect is that a user who redundantly re-specifies the active default port on remount gets -EINVAL with a clear message.

---
 fs/smb/client/fs_context.c | 141 +++++++++++++++++++++++++++++++++++++
 1 file changed, 141 insertions(+)
fs/smb/client/fs_context.c +141 -0
index 1a68ae058166..d81bdc875580 100644
@@ -1110,6 +1110,147 @@ static int smb3_verify_reconfigure_ctx(struct fs_context *fc,
 		cifs_errorf(fc, "can not change nbsessinit during remount\n");
 		return -EINVAL;
 	}
+	if (new_ctx->compress != old_ctx->compress) {
+		cifs_errorf(fc, "can not change compress during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->noblocksnd != old_ctx->noblocksnd) {
+		cifs_errorf(fc, "can not change noblocksend during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->noautotune != old_ctx->noautotune) {
+		cifs_errorf(fc, "can not change noautotune during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->no_sparse != old_ctx->no_sparse) {
+		cifs_errorf(fc, "can not change nosparse during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->nodelete != old_ctx->nodelete) {
+		cifs_errorf(fc, "can not change nodelete during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->cruid_specified &&
+	    !uid_eq(new_ctx->cred_uid, old_ctx->cred_uid)) {
+		cifs_errorf(fc, "can not change cruid during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->port != old_ctx->port) {
+		cifs_errorf(fc, "can not change port during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->min_offload != old_ctx->min_offload) {
+		cifs_errorf(fc, "can not change min_enc_offload during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->echo_interval != old_ctx->echo_interval) {
+		cifs_errorf(fc, "can not change echo_interval during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->snapshot_time != old_ctx->snapshot_time) {
+		cifs_errorf(fc, "can not change snapshot during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->max_credits != old_ctx->max_credits) {
+		cifs_errorf(fc, "can not change max_credits during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->handle_timeout != old_ctx->handle_timeout) {
+		cifs_errorf(fc, "can not change handletimeout during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->got_ip &&
+	    !cifs_match_ipaddr((struct sockaddr *)&new_ctx->dstaddr,
+			       (struct sockaddr *)&old_ctx->dstaddr)) {
+		cifs_errorf(fc, "can not change ip during remount\n");
+		return -EINVAL;
+	}
+	if (!cifs_match_ipaddr((struct sockaddr *)&new_ctx->srcaddr,
+			       (struct sockaddr *)&old_ctx->srcaddr)) {
+		cifs_errorf(fc, "can not change srcaddr during remount\n");
+		return -EINVAL;
+	}
+	if (memcmp(new_ctx->source_rfc1001_name, old_ctx->source_rfc1001_name,
+		   RFC1001_NAME_LEN)) {
+		cifs_errorf(fc, "can not change netbiosname during remount\n");
+		return -EINVAL;
+	}
+	if (memcmp(new_ctx->target_rfc1001_name, old_ctx->target_rfc1001_name,
+		   RFC1001_NAME_LEN)) {
+		cifs_errorf(fc, "can not change servern during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->got_version &&
+	    (new_ctx->ops != old_ctx->ops || new_ctx->vals != old_ctx->vals)) {
+		cifs_errorf(fc, "can not change vers during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->witness != old_ctx->witness) {
+		cifs_errorf(fc, "can not change witness during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->rootfs != old_ctx->rootfs) {
+		cifs_errorf(fc, "can not change rootfs during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->linux_ext != old_ctx->linux_ext ||
+	    new_ctx->no_linux_ext != old_ctx->no_linux_ext) {
+		cifs_errorf(fc, "can not change unix during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->nocase != old_ctx->nocase) {
+		cifs_errorf(fc, "can not change nocase during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->intr != old_ctx->intr) {
+		cifs_errorf(fc, "can not change intr during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->no_psx_acl != old_ctx->no_psx_acl) {
+		cifs_errorf(fc, "can not change acl during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->local_lease != old_ctx->local_lease) {
+		cifs_errorf(fc, "can not change locallease during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->sign != old_ctx->sign) {
+		cifs_errorf(fc, "can not change sign during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->ignore_signature != old_ctx->ignore_signature) {
+		cifs_errorf(fc, "can not change ignore_signature (signloosely) during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->seal != old_ctx->seal) {
+		cifs_errorf(fc, "can not change seal during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->nosharesock != old_ctx->nosharesock) {
+		cifs_errorf(fc, "can not change nosharesock during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->persistent != old_ctx->persistent ||
+	    new_ctx->nopersistent != old_ctx->nopersistent) {
+		cifs_errorf(fc, "can not change persistenthandles during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->resilient != old_ctx->resilient) {
+		cifs_errorf(fc, "can not change resilienthandles during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->sockopt_tcp_nodelay != old_ctx->sockopt_tcp_nodelay) {
+		cifs_errorf(fc, "can not change tcpnodelay during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->domainauto != old_ctx->domainauto) {
+		cifs_errorf(fc, "can not change domainauto during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->rdma != old_ctx->rdma) {
+		cifs_errorf(fc, "can not change rdma during remount\n");
+		return -EINVAL;
+	}
 
 	return 0;
 }
Signature · 1 line
2.43.0
#12 lore raw

PATCH v2 3/8 smb: client: sync retrans on remount

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

The retrans mount option controls how many times the client retries a request before giving up. Remount stored the new value in cifs_sb->ctx but never pushed it to server->retrans, so the live connection kept using the old count.

Add smb3_sync_server_opts() to copy ctx options that live on TCP_Server_Info into the running server after a successful remount. For now it handles retrans; later patches extend it to other server-level knobs. Because the earlier "sync runtime state into ctx" patch seeds the baseline retrans from the live server, a bare 'mount -o remount' carries the current value forward and the parser only overwrites it when the user passes retrans=N -- so an explicit retrans=0 now correctly resets to the compile-time default.

server->retrans is a plain word shared by all mounts on the connection, so reconfiguring one mount changes the value seen by the others. That matches existing semantics: cifs_show_options() already reports server->retrans per mount and the retry path always uses the server's value. Its access sites are each a single independent read or write, so they use READ_ONCE()/WRITE_ONCE() to avoid tearing rather than taking srv_lock.

---
 fs/smb/client/cifsfs.c     |  6 ++++--
 fs/smb/client/connect.c    |  4 ++--
 fs/smb/client/fs_context.c | 30 ++++++++++++++++++++++++++++++
 fs/smb/client/sess.c       |  2 +-
 fs/smb/client/smb2ops.c    |  2 +-
 5 files changed, 38 insertions(+), 6 deletions(-)
fs/smb/client/cifsfs.c +4 -2
index a1dacc7d8f74..07d58b26f455 100644
@@ -627,6 +627,7 @@ cifs_show_options(struct seq_file *s, struct dentry *root)
 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 	struct sockaddr *srcaddr;
 	unsigned int sbflags;
+	unsigned int retrans;
 
 	srcaddr = (struct sockaddr *)&tcon->ses->server->srcaddr;
 
@@ -782,8 +783,9 @@ cifs_show_options(struct seq_file *s, struct dentry *root)
 		seq_printf(s, ",rasize=%u", cifs_sb->ctx->rasize);
 	if (tcon->ses->server->min_offload)
 		seq_printf(s, ",esize=%u", tcon->ses->server->min_offload);
-	if (tcon->ses->server->retrans)
-		seq_printf(s, ",retrans=%u", tcon->ses->server->retrans);
+	retrans = READ_ONCE(tcon->ses->server->retrans);
+	if (retrans)
+		seq_printf(s, ",retrans=%u", retrans);
 	seq_printf(s, ",echo_interval=%lu",
 			tcon->ses->server->echo_interval / HZ);
 
fs/smb/client/connect.c +2 -2
index ba749ec25a59..b5d257a83b4f 100644
@@ -1658,7 +1658,7 @@ static int match_server(struct TCP_Server_Info *server,
 	if (server->min_offload != ctx->min_offload)
 		return 0;
 
-	if (server->retrans != ctx->retrans)
+	if (READ_ONCE(server->retrans) != ctx->retrans)
 		return 0;
 
 	return 1;
@@ -1873,7 +1873,7 @@ cifs_get_tcp_session(struct smb3_fs_context *ctx,
 	 */
 	__module_get(THIS_MODULE);
 	tcp_ses->min_offload = ctx->min_offload;
-	tcp_ses->retrans = ctx->retrans;
+	WRITE_ONCE(tcp_ses->retrans, ctx->retrans);
 	/*
 	 * at this point we are the only ones with the pointer
 	 * to the struct since the kernel thread not created yet
fs/smb/client/fs_context.c +30 -0
index d81bdc875580..2fb40e9e1652 100644
@@ -964,6 +964,16 @@ static int smb3_sync_ctx_from_runtime(struct cifs_sb_info *cifs_sb,
 	ctx->nosharesock = server->nosharesock;
 	spin_unlock(&server->srv_lock);
 
+	/*
+	 * retrans lives on the shared TCP_Server_Info and can be updated by
+	 * a remount of any mount sharing the connection.  Sync it from the
+	 * live server so a bare remount carries the current value forward
+	 * instead of writing a stale per-mount copy back in
+	 * smb3_sync_server_opts().  Read with READ_ONCE to pair with the
+	 * lockless WRITE_ONCE writers and the runtime retry-path reader.
+	 */
+	ctx->retrans = READ_ONCE(server->retrans);
+
 	/*
 	 * These connection-tied options live on the shared TCP_Server_Info and
 	 * are reported by cifs_show_options() from server->*, not from the
@@ -1318,6 +1328,24 @@ static void smb3_sync_ses_chan_max(struct cifs_ses *ses, size_t max_channels)
 	spin_unlock(&ses->chan_lock);
 }
 
+/*
+ * Synchronize server-level options that are stored on TCP_Server_Info
+ * at mount time.  These fields are consulted at runtime (retry logic)
+ * so remount needs to update the live server struct in addition to
+ * cifs_sb->ctx.  Note these live on TCP_Server_Info and are therefore
+ * shared across all mounts to the same server; reconfiguring one mount
+ * updates the value seen by every other mount sharing the connection,
+ * matching how cifs_show_options() and the runtime retry path already
+ * read them unsynchronized from the server struct.
+ */
+static void smb3_sync_server_opts(struct cifs_sb_info *cifs_sb)
+{
+	struct TCP_Server_Info *server = cifs_sb_master_tcon(cifs_sb)->ses->server;
+	struct smb3_fs_context *ctx = cifs_sb->ctx;
+
+	WRITE_ONCE(server->retrans, ctx->retrans);
+}
+
 static int smb3_reconfigure(struct fs_context *fc)
 {
 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
@@ -1516,6 +1544,8 @@ static int smb3_reconfigure(struct fs_context *fc)
 	if (!rc)
 		rc = dfs_cache_remount_fs(cifs_sb);
 #endif
+	if (!rc)
+		smb3_sync_server_opts(cifs_sb);
 
 	return rc;
 
fs/smb/client/sess.c +1 -1
index 7cf7dd104f7c..dd5a1531808d 100644
@@ -557,7 +557,7 @@ cifs_ses_add_channel(struct cifs_ses *ses,
 	ctx->ignore_signature = ses->server->ignore_signature;
 	ctx->leaf_fullpath = ses->server->leaf_fullpath;
 	ctx->rootfs = ses->server->noblockcnt;
-	ctx->retrans = ses->server->retrans;
+	ctx->retrans = READ_ONCE(ses->server->retrans);
 
 	/*
 	 * This will be used for encoding/decoding user/domain/pw
fs/smb/client/smb2ops.c +1 -1
index 192649fec25d..0debeb36447d 100644
@@ -2837,7 +2837,7 @@ bool smb2_should_replay(struct cifs_tcon *tcon,
 	if (!pretries || !pcur_sleep)
 		return false;
 
-	if (tcon->retry || (*pretries)++ < tcon->ses->server->retrans) {
+	if (tcon->retry || (*pretries)++ < READ_ONCE(tcon->ses->server->retrans)) {
 		/* Update sleep time for exponential backoff */
 		if (!(*pcur_sleep))
 			(*pcur_sleep) = 1;
Signature · 1 line
2.43.0
#13 lore raw

PATCH v2 4/8 smb: client: block cache=ro and cache=singleclient on remount

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

cache=ro and cache=singleclient are mount-time environment declarations where the admin promises that the share is read-only or exclusively accessed. The client bypasses server-based coherency (oplocks/leases) and caches aggressively based on this promise.

These modes were intentionally excluded from smb3_update_mnt_flags() when it was introduced in commit 2d39f50c2b15 ("cifs: move update of flags into a separate function") — only cache=strict, cache=none and cache=loose were made reconfigurable. However, remount currently silently accepts cache=ro and cache=singleclient without actually applying them, which is confusing.

Add explicit checks in smb3_verify_reconfigure_ctx() to reject attempts to change these options during remount with a clear error message.

---
 fs/smb/client/fs_context.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
fs/smb/client/fs_context.c +13 -0
index 2fb40e9e1652..4a319c113196 100644
@@ -1261,6 +1261,19 @@ static int smb3_verify_reconfigure_ctx(struct fs_context *fc,
 		cifs_errorf(fc, "can not change rdma during remount\n");
 		return -EINVAL;
 	}
+	/*
+	 * cache=ro sets cache_ro and cache=singleclient sets cache_rw; the
+	 * other cache modes (strict/loose/none) leave both clear and are
+	 * handled separately in smb3_update_mnt_flags().
+	 */
+	if (new_ctx->cache_ro != old_ctx->cache_ro) {
+		cifs_errorf(fc, "can not change cache=ro during remount\n");
+		return -EINVAL;
+	}
+	if (new_ctx->cache_rw != old_ctx->cache_rw) {
+		cifs_errorf(fc, "can not change cache=singleclient during remount\n");
+		return -EINVAL;
+	}
 
 	return 0;
 }
Signature · 1 line
2.43.0
#14 lore raw

PATCH v2 5/8 smb: client: apply rasize on remount

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

rasize is accepted during remount parsing but sb->s_bdi->ra_pages is only set in cifs_read_super() at mount time. A remount with a new rasize value silently has no effect on the readahead window.

Update ra_pages in smb3_reconfigure() after the context has been duplicated, using the same logic as cifs_read_super(): if rasize is set, use it directly; otherwise fall back to 2 * rsize.

Not addressed here (pre-existing, independent of the rasize change):
a bare remount realigns a sub-PAGE_SIZE server-negotiated rsize/wsize up to PAGE_SIZE via CIFS_ALIGN_*; and smb3_reconfigure() briefly NULLs cifs_sb->ctx->source through STEAL_STRING() before restoring it, so a concurrent DFS reconnect reading cifs_sb->ctx->source locklessly could observe NULL. Both predate this series; the latter shares the unserialized cifs_sb->ctx root cause described in patch 1. Left for separate changes.

---
 fs/smb/client/fs_context.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/smb/client/fs_context.c +9 -2
index 4a319c113196..ee8e38c3b7f4 100644
@@ -1368,7 +1368,7 @@ static int smb3_reconfigure(struct fs_context *fc)
 	struct dentry *root = fc->root;
 	struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
 	struct cifs_ses *ses = cifs_sb_master_tcon(cifs_sb)->ses;
-	unsigned int rsize = ctx->rsize, wsize = ctx->wsize;
+	unsigned int rsize = ctx->rsize, wsize = ctx->wsize, rasize = ctx->rasize;
 	char *new_password = NULL, *new_password2 = NULL;
 	bool need_recon = false;
 	bool need_mchan_update;
@@ -1462,9 +1462,10 @@ static int smb3_reconfigure(struct fs_context *fc)
 		STEAL_STRING_SENSITIVE(cifs_sb, ctx, password2);
 	}
 
-	/* if rsize or wsize not passed in on remount, use previous values */
+	/* if rsize, wsize, or rasize not passed in on remount, use previous values */
 	ctx->rsize = rsize ? CIFS_ALIGN_RSIZE(fc, rsize) : cifs_sb->ctx->rsize;
 	ctx->wsize = wsize ? CIFS_ALIGN_WSIZE(fc, wsize) : cifs_sb->ctx->wsize;
+	ctx->rasize = rasize ? rasize : cifs_sb->ctx->rasize;
 
 	new_ctx = kzalloc_obj(*new_ctx);
 	if (!new_ctx) {
@@ -1553,6 +1554,12 @@ static int smb3_reconfigure(struct fs_context *fc)
 	smb3_cleanup_fs_context(old_ctx);
 	old_ctx = NULL;
 	smb3_update_mnt_flags(cifs_sb);
+
+	if (cifs_sb->ctx->rasize)
+		root->d_sb->s_bdi->ra_pages = cifs_sb->ctx->rasize / PAGE_SIZE;
+	else
+		root->d_sb->s_bdi->ra_pages = 2 * (cifs_sb->ctx->rsize / PAGE_SIZE);
+
 #ifdef CONFIG_CIFS_DFS_UPCALL
 	if (!rc)
 		rc = dfs_cache_remount_fs(cifs_sb);
Signature · 1 line
2.43.0
#15 lore raw

PATCH v2 6/8 smb: client: move struct tcon_list to cifsglob.h

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

struct tcon_list is a small list-node wrapper that pairs a list_head with a cifs_tcon pointer, used to safely iterate tcons under a superblock outside tlink_tree_lock.

It is currently file-static in misc.c, used only by cifs_close_all_deferred_files_sb(). Move it next to the similar struct file_list in cifsglob.h so that other _sb() iteration helpers can reuse it instead of redefining the same shape locally.

No functional change.

---
 fs/smb/client/cifsglob.h | 5 +++++
 fs/smb/client/misc.c     | 5 -----
 2 files changed, 5 insertions(+), 5 deletions(-)
fs/smb/client/cifsglob.h +5 -0
index 79e4e84f8985..3bd2a588e7c4 100644
@@ -1812,6 +1812,11 @@ struct file_list {
 	struct cifsFileInfo *cfile;
 };
 
+struct tcon_list {
+	struct list_head entry;
+	struct cifs_tcon *tcon;
+};
+
 struct cifs_mount_ctx {
 	struct cifs_sb_info *cifs_sb;
 	struct smb3_fs_context *fs_ctx;
fs/smb/client/misc.c +0 -5
index 46e1382e8e04..7fb38f92cd5a 100644
@@ -28,11 +28,6 @@
 #include "fs_context.h"
 #include "cached_dir.h"
 
-struct tcon_list {
-	struct list_head entry;
-	struct cifs_tcon *tcon;
-};
-
 /* The xid serves as a useful identifier for each incoming vfs request,
    in a similar way to the mid which is useful to track each sent smb,
    and CurrentXid can also provide a running counter (although it
Signature · 1 line
2.43.0
#16 lore raw

PATCH v2 7/8 smb: client: allow nolease option to be reconfigured on remount

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

Changing nolease via remount is silently accepted but has no effect:
the value is never propagated to the live tcons, and state from before the switch keeps using the old behavior.

Make nolease take effect on remount:

- Add smb3_sync_tcon_opts(), called from smb3_reconfigure() after the context is updated, to propagate ctx->no_lease to every tcon under the superblock (walking tlink_tree under tlink_tree_lock) so future opens honor the new setting.

- On switch to nolease, drop deferred file handles via cifs_close_all_deferred_files_sb() and evict cached directory fids via the new invalidate_all_cached_dirs_sb(), since each holds a lease.

invalidate_all_cached_dirs() gains a close_handles argument: the live remount passes true so smb2_close_cached_fid() actually sends SMB2_close to release the server-side directory handle and lease; the session-loss/teardown callers pass false, keeping their old behavior. invalidate_all_cached_dirs_sb() counts the tcons under the lock, allocates the pointer array with GFP_KERNEL outside it, then re-takes the lock to collect a reference on each tcon and invalidates outside the lock (it can sleep).

Move tcon->no_lease out of the bitfield into a plain bool: as a runtime writer it would otherwise share a word with need_reconnect/ need_reopen_files, and a bitfield read-modify-write could clobber a concurrent lockless update of those.

Existing open handles keep their leases until the server breaks them or userspace closes the file -- nolease only governs new opens.

Not addressed here (pre-existing, unrelated to nolease):
cfids_laundromat_worker() can resurrect a tcon from tc_count 0 without a TID_EXITING check, racing cifs_put_tcon(); and the cifs_debug dirs knob still invalidates with close_handles=false. These are left for separate changes.

---
 fs/smb/client/cached_dir.c | 89 +++++++++++++++++++++++++++++++++++---
 fs/smb/client/cached_dir.h |  4 +-
 fs/smb/client/cifs_debug.c |  2 +-
 fs/smb/client/cifsglob.h   |  2 +-
 fs/smb/client/file.c       |  2 +-
 fs/smb/client/fs_context.c | 54 +++++++++++++++++++++++
 fs/smb/client/smb2pdu.c    |  2 +-
 fs/smb/client/trace.h      |  2 +
 8 files changed, 145 insertions(+), 12 deletions(-)
fs/smb/client/cached_dir.c +82 -7
index 88d5e9a32f28..f85d2d1dbbe3 100644
@@ -598,10 +598,17 @@ void close_all_cached_dirs(struct cifs_sb_info *cifs_sb)
 }
 
 /*
- * Invalidate all cached dirs when a TCON has been reset
- * due to a session loss.
+ * Invalidate all cached dirs on a TCON, moving them to the dying list
+ * for the laundromat to clean up.
+ *
+ * @close_handles: if true, the connection is still live (e.g. remount),
+ * so leave cfid->is_open set and let smb2_close_cached_fid() send
+ * SMB2_close to release the server-side directory handle and lease.  If
+ * false (the session-loss teardown case), mark the cfids closed so the
+ * doomed SMB2_close is skipped -- the server has already dropped them.
  */
-void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync)
+void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync,
+				bool close_handles)
 {
 	struct cached_fids *cfids = tcon->cfids;
 	struct cached_fid *cfid, *q;
@@ -610,15 +617,16 @@ void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync)
 		return;
 
 	/*
-	 * Mark all the cfids as closed, and move them to the cfids->dying list.
-	 * They'll be cleaned up by laundromat.  Take a reference to each cfid
-	 * during this process.
+	 * Move all the cfids to the cfids->dying list.  They'll be cleaned
+	 * up by laundromat.  Take a reference to each cfid during this
+	 * process.
 	 */
 	spin_lock(&cfids->cfid_list_lock);
 	list_for_each_entry_safe(cfid, q, &cfids->entries, entry) {
 		list_move(&cfid->entry, &cfids->dying);
 		cfids->num_entries--;
-		cfid->is_open = false;
+		if (!close_handles)
+			cfid->is_open = false;
 		cfid->on_list = false;
 		if (cfid->has_lease) {
 			/*
@@ -637,6 +645,73 @@ void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync)
 		flush_delayed_work(&cfids->laundromat_work);
 }
 
+/*
+ * Invalidate cached directory entries across all tcons under a
+ * superblock.  Count the tcons first so the pointer array can be
+ * allocated with GFP_KERNEL outside tlink_tree_lock: allocating
+ * per-tcon under the spinlock would force GFP_ATOMIC and, on failure,
+ * silently leave some tcons with stale leases after a nolease remount.
+ * References are then taken under tlink_tree_lock and the cached dirs
+ * closed outside the spinlock since that can sleep.  Holding a tc_count
+ * reference prevents the tcon from being freed by tlink_expire_delayed()
+ * between dropping the spinlock and the call.
+ *
+ * Called on remount while the connection is live (e.g. switching to
+ * nolease), so pass close_handles=true to actually release the
+ * server-side directory handles and their leases.
+ */
+void invalidate_all_cached_dirs_sb(struct cifs_sb_info *cifs_sb)
+{
+	struct rb_root *root = &cifs_sb->tlink_tree;
+	struct rb_node *node;
+	struct cifs_tcon *tcon;
+	struct tcon_link *tlink;
+	struct cifs_tcon **tcons;
+	unsigned int i, n = 0, count = 0;
+
+	spin_lock(&cifs_sb->tlink_tree_lock);
+	for (node = rb_first(root); node; node = rb_next(node)) {
+		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
+		if (!IS_ERR(tlink_tcon(tlink)))
+			count++;
+	}
+	spin_unlock(&cifs_sb->tlink_tree_lock);
+
+	if (!count)
+		return;
+
+	/*
+	 * Best effort: if the snapshot array can't be allocated, skip the
+	 * eviction.  New opens still honor the updated nolease via the
+	 * tcon->no_lease propagation done by the caller.
+	 */
+	tcons = kcalloc(count, sizeof(*tcons), GFP_KERNEL);
+	if (!tcons)
+		return;
+
+	/* n < count bounds the walk to the snapshot size if tcons are added */
+	spin_lock(&cifs_sb->tlink_tree_lock);
+	for (node = rb_first(root); node && n < count; node = rb_next(node)) {
+		tlink = rb_entry(node, struct tcon_link, tl_rbnode);
+		tcon = tlink_tcon(tlink);
+		if (IS_ERR(tcon))
+			continue;
+		spin_lock(&tcon->tc_lock);
+		++tcon->tc_count;
+		trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
+				    netfs_trace_tcon_ref_get_cached_inval_sb);
+		spin_unlock(&tcon->tc_lock);
+		tcons[n++] = tcon;
+	}
+	spin_unlock(&cifs_sb->tlink_tree_lock);
+
+	for (i = 0; i < n; i++) {
+		invalidate_all_cached_dirs(tcons[i], true, true);
+		cifs_put_tcon(tcons[i], netfs_trace_tcon_ref_put_cached_inval_sb);
+	}
+	kfree(tcons);
+}
+
 static void
 cached_dir_offload_close(struct work_struct *work)
 {
fs/smb/client/cached_dir.h +3 -1
index fc756836da95..3b86339ca884 100644
@@ -90,7 +90,9 @@ void close_cached_dir(struct cached_fid *cfid);
 void drop_cached_dir_by_name(const unsigned int xid, struct cifs_tcon *tcon,
 			     const char *name, struct cifs_sb_info *cifs_sb);
 void close_all_cached_dirs(struct cifs_sb_info *cifs_sb);
-void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync);
+void invalidate_all_cached_dirs_sb(struct cifs_sb_info *cifs_sb);
+void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync,
+				bool close_handles);
 bool cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16]);
 
 #endif			/* _CACHED_DIR_H */
fs/smb/client/cifs_debug.c +1 -1
index 4ed4f55a0bb7..28b0d433810f 100644
@@ -383,7 +383,7 @@ static ssize_t cifs_debug_dirs_proc_write(struct file *file, const char __user *
 				if (cifs_ses_exiting(ses))
 					continue;
 				list_for_each_entry(tcon, &ses->tcon_list, tcon_list)
-					invalidate_all_cached_dirs(tcon, false);
+					invalidate_all_cached_dirs(tcon, false, false);
 			}
 		}
 		spin_unlock(&cifs_tcp_ses_lock);
fs/smb/client/cifsglob.h +1 -1
index 3bd2a588e7c4..2c5bc4dee5b2 100644
@@ -1265,9 +1265,9 @@ struct cifs_tcon {
 	bool need_reopen_files:1; /* need to reopen tcon file handles */
 	bool use_resilient:1; /* use resilient instead of durable handles */
 	bool use_persistent:1; /* use persistent instead of durable handles */
-	bool no_lease:1;    /* Do not request leases on files or directories */
 	bool use_witness:1; /* use witness protocol */
 	bool dummy:1; /* dummy tcon used for reconnecting channels */
+	bool no_lease;    /* Do not request leases on files or directories */
 	__le32 capabilities;
 	__u32 share_flags;
 	__u32 maximal_access;
fs/smb/client/file.c +1 -1
index ac89c1ba56b1..852687916c4f 100644
@@ -399,7 +399,7 @@ cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
 	}
 	spin_unlock(&tcon->open_file_lock);
 
-	invalidate_all_cached_dirs(tcon, true);
+	invalidate_all_cached_dirs(tcon, true, false);
 	spin_lock(&tcon->tc_lock);
 	if (tcon->status == TID_IN_FILES_INVALIDATE)
 		tcon->status = TID_NEED_TCON;
fs/smb/client/fs_context.c +54 -0

72 lines not on this page · raw message

fs/smb/client/smb2pdu.c +1 -1
index ee63b8547728..baef7efd78c7 100644
@@ -2304,7 +2304,7 @@ SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
 	}
 	spin_unlock(&ses->chan_lock);
 
-	invalidate_all_cached_dirs(tcon, true);
+	invalidate_all_cached_dirs(tcon, true, false);
 
 	rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, server,
 				 (void **) &req,
fs/smb/client/trace.h +2 -0
index 5b21ad3c15fb..1e61a36759ed 100644
@@ -173,6 +173,7 @@
 	EM(netfs_trace_tcon_ref_free_ipc,		"FRE Ipc   ") \
 	EM(netfs_trace_tcon_ref_free_ipc_fail,		"FRE Ipc-F ") \
 	EM(netfs_trace_tcon_ref_free_reconnect_server,	"FRE Reconn") \
+	EM(netfs_trace_tcon_ref_get_cached_inval_sb,	"GET Ch-IvS") \
 	EM(netfs_trace_tcon_ref_get_cached_laundromat,	"GET Ch-Lau") \
 	EM(netfs_trace_tcon_ref_get_cached_lease_break,	"GET Ch-Lea") \
 	EM(netfs_trace_tcon_ref_get_cancelled_close,	"GET Cn-Cls") \
@@ -186,6 +187,7 @@
 	EM(netfs_trace_tcon_ref_new_ipc,		"NEW Ipc   ") \
 	EM(netfs_trace_tcon_ref_new_reconnect_server,	"NEW Reconn") \
 	EM(netfs_trace_tcon_ref_put_cached_close,	"PUT Ch-Cls") \
+	EM(netfs_trace_tcon_ref_put_cached_inval_sb,	"PUT Ch-IvS") \
 	EM(netfs_trace_tcon_ref_put_cancelled_close,	"PUT Cn-Cls") \
 	EM(netfs_trace_tcon_ref_put_cancelled_close_fid, "PUT Cn-Fid") \
 	EM(netfs_trace_tcon_ref_put_cancelled_mid,	"PUT Cn-Mid") \
Signature · 1 line
2.43.0
maintainer
#17 lore raw

Re: [PATCH v2 7/8] smb: client: allow nolease option to be reconfigured on remount

Quoted 33 lines
fs/smb/client/cached_dir.c +33 -0
+/*
+ * Invalidate cached directory entries across all tcons under a
+ * superblock.  Count the tcons first so the pointer array can be
+ * allocated with GFP_KERNEL outside tlink_tree_lock: allocating
+ * per-tcon under the spinlock would force GFP_ATOMIC and, on failure,
+ * silently leave some tcons with stale leases after a nolease remount.
+ * References are then taken under tlink_tree_lock and the cached dirs
+ * closed outside the spinlock since that can sleep.  Holding a tc_count
+ * reference prevents the tcon from being freed by tlink_expire_delayed()
+ * between dropping the spinlock and the call.
+ *
+ * Called on remount while the connection is live (e.g. switching to
+ * nolease), so pass close_handles=true to actually release the
+ * server-side directory handles and their leases.
+ */
+void invalidate_all_cached_dirs_sb(struct cifs_sb_info *cifs_sb)
+{
+       struct rb_root *root = &cifs_sb->tlink_tree;
+       struct rb_node *node;
+       struct cifs_tcon *tcon;
+       struct tcon_link *tlink;
+       struct cifs_tcon **tcons;
+       unsigned int i, n = 0, count = 0;
+
+       spin_lock(&cifs_sb->tlink_tree_lock);
+       for (node = rb_first(root); node; node = rb_next(node)) {
+               tlink = rb_entry(node, struct tcon_link, tl_rbnode);
+               if (!IS_ERR(tlink_tcon(tlink)))
+                       count++;
+       }
+       spin_unlock(&cifs_sb->tlink_tree_lock);
+
+       if (!count)

fs/smb/client/cached_dir.c void invalidate_all_cached_dirs(struct cifs_tcon *tcon, bool sync) · line 690

Quoted 10 lines
fs/smb/client/cached_dir.c +10 -0
+               return;
+
+       /*
+        * Best effort: if the snapshot array can't be allocated, skip the
+        * eviction.  New opens still honor the updated nolease via the
+        * tcon->no_lease propagation done by the caller.
+        */
+       tcons = kcalloc(count, sizeof(*tcons), GFP_KERNEL);
+       if (!tcons)
+               return;

If this allocation fails, the function silently skips evicting cached directory FIDs while the remount still succeeds. Such a FID may then be reused after nolease is enabled. So please do not silently return here.

#18 lore raw

PATCH v2 8/8 smb: client: take tc_lock around tc_count bump in cached_dir_lease_break()

In reply to [email protected]

From: Rajasi Mandal <[email protected]>

Every other tcon reference-count update takes tcon->tc_lock:
cifs_put_tcon(), the reconnect/mount paths in connect.c, misc.c, smb2misc.c, smb2ops.c, smb2pdu.c, smb2transport.c, cifs_swn.c, and the two other cached-dir sites (invalidate_all_cached_dirs_sb() and cfids_laundromat_worker()). cached_dir_lease_break() is the sole
exception: it bumps tcon->tc_count with only cfids->cfid_list_lock held.

A lease break can therefore race another tc_count writer (e.g. a concurrent put/get) and lose an update, which under-counts the tcon and risks an early teardown / use-after-free, or over-counts and leaks it.

Take tcon->tc_lock across the increment and its tracepoint, matching every other site. The lock is nested inside cfid_list_lock; that ordering is consistent with the rest of the file (tc_lock is a leaf lock taken only for brief tcon field updates, and no path acquires cfid_list_lock while holding tc_lock), so it introduces no new ordering.

---
 fs/smb/client/cached_dir.c | 2 ++
 1 file changed, 2 insertions(+)
fs/smb/client/cached_dir.c +2 -0
index f85d2d1dbbe3..c9b6eb56b37b 100644
@@ -766,9 +766,11 @@ bool cached_dir_lease_break(struct cifs_tcon *tcon, __u8 lease_key[16])
 			cfid->on_list = false;
 			cfids->num_entries--;
 
+			spin_lock(&tcon->tc_lock);
 			++tcon->tc_count;
 			trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
 					    netfs_trace_tcon_ref_get_cached_lease_break);
+			spin_unlock(&tcon->tc_lock);
 			queue_work(cfid_put_wq, &cfid->put_work);
 			spin_unlock(&cfids->cfid_list_lock);
 			return true;
Signature · 1 line
2.43.0