]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
smb: client: Store original IO parameters and prevent zero IO sizes
authorWang Zhaolong <wangzhaolong1@huawei.com>
Mon, 31 Mar 2025 13:33:14 +0000 (21:33 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 4 Jun 2025 12:41:54 +0000 (14:41 +0200)
[ Upstream commit 287906b20035a04a234d1a3c64f760a5678387be ]

During mount option processing and negotiation with the server, the
original user-specified rsize/wsize values were being modified directly.
This makes it impossible to recover these values after a connection
reset, leading to potential degraded performance after reconnection.

The other problem is that When negotiating read and write sizes, there are
cases where the negotiated values might calculate to zero, especially
during reconnection when server->max_read or server->max_write might be
reset. In general, these values come from the negotiation response.
According to MS-SMB2 specification, these values should be at least 65536
bytes.

This patch improves IO parameter handling:

1. Adds vol_rsize and vol_wsize fields to store the original user-specified
   values separately from the negotiated values
2. Uses got_rsize/got_wsize flags to determine if values were
   user-specified rather than checking for non-zero values, which is more
   reliable
3. Adds a prevent_zero_iosize() helper function to ensure IO sizes are
   never negotiated down to zero, which could happen in edge cases like
   when server->max_read/write is zero

The changes make the CIFS client more resilient to unusual server
responses and reconnection scenarios, preventing potential failures
when IO sizes are calculated to be zero.

Signed-off-by: Wang Zhaolong <wangzhaolong1@huawei.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/smb/client/fs_context.c
fs/smb/client/fs_context.h
fs/smb/client/smb1ops.c
fs/smb/client/smb2ops.c
fs/smb/common/smb2pdu.h

index d2e291ef104ec0d6a985f85ecded88e9e91e871d..137d03781d5268d38a4c4c04e1cf0dce6475dd93 100644 (file)
@@ -1249,6 +1249,7 @@ static int smb3_fs_context_parse_param(struct fs_context *fc,
        case Opt_rsize:
                ctx->rsize = result.uint_32;
                ctx->got_rsize = true;
+               ctx->vol_rsize = ctx->rsize;
                break;
        case Opt_wsize:
                ctx->wsize = result.uint_32;
@@ -1264,6 +1265,7 @@ static int smb3_fs_context_parse_param(struct fs_context *fc,
                                         ctx->wsize, PAGE_SIZE);
                        }
                }
+               ctx->vol_wsize = ctx->wsize;
                break;
        case Opt_acregmax:
                if (result.uint_32 > CIFS_MAX_ACTIMEO / HZ) {
index bbd2063ab838d34197021faceec72bf1043f369f..d0a2043ea44682fdcc46ccaf01141fcc8af674c3 100644 (file)
@@ -253,6 +253,9 @@ struct smb3_fs_context {
        bool use_client_guid:1;
        /* reuse existing guid for multichannel */
        u8 client_guid[SMB2_CLIENT_GUID_SIZE];
+       /* User-specified original r/wsize value */
+       unsigned int vol_rsize;
+       unsigned int vol_wsize;
        unsigned int bsize;
        unsigned int rasize;
        unsigned int rsize;
index 280885dd6bf080e670a1b403d66ce92e64c7b349..0ba0f680205c1d7a86c4cbad843dd5f74a3beaba 100644 (file)
@@ -437,8 +437,8 @@ cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
        unsigned int wsize;
 
        /* start with specified wsize, or default */
-       if (ctx->wsize)
-               wsize = ctx->wsize;
+       if (ctx->got_wsize)
+               wsize = ctx->vol_wsize;
        else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
                wsize = CIFS_DEFAULT_IOSIZE;
        else
@@ -490,7 +490,7 @@ cifs_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
        else
                defsize = server->maxBuf - sizeof(READ_RSP);
 
-       rsize = ctx->rsize ? ctx->rsize : defsize;
+       rsize = ctx->got_rsize ? ctx->vol_rsize : defsize;
 
        /*
         * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
index cead96454bb3ddc25380731cfd2c43fd064a30cd..4e3eacbec96d147dfeadfdf43d2ed7ec5e8a3d01 100644 (file)
@@ -431,6 +431,17 @@ smb2_negotiate(const unsigned int xid,
        return rc;
 }
 
+static inline unsigned int
+prevent_zero_iosize(unsigned int size, const char *type)
+{
+       if (size == 0) {
+               cifs_dbg(VFS, "SMB: Zero %ssize calculated, using minimum value %u\n",
+                        type, CIFS_MIN_DEFAULT_IOSIZE);
+               return CIFS_MIN_DEFAULT_IOSIZE;
+       }
+       return size;
+}
+
 static unsigned int
 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
 {
@@ -438,12 +449,12 @@ smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
        unsigned int wsize;
 
        /* start with specified wsize, or default */
-       wsize = ctx->wsize ? ctx->wsize : CIFS_DEFAULT_IOSIZE;
+       wsize = ctx->got_wsize ? ctx->vol_wsize : CIFS_DEFAULT_IOSIZE;
        wsize = min_t(unsigned int, wsize, server->max_write);
        if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
                wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
 
-       return wsize;
+       return prevent_zero_iosize(wsize, "w");
 }
 
 static unsigned int
@@ -453,7 +464,7 @@ smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
        unsigned int wsize;
 
        /* start with specified wsize, or default */
-       wsize = ctx->wsize ? ctx->wsize : SMB3_DEFAULT_IOSIZE;
+       wsize = ctx->got_wsize ? ctx->vol_wsize : SMB3_DEFAULT_IOSIZE;
        wsize = min_t(unsigned int, wsize, server->max_write);
 #ifdef CONFIG_CIFS_SMB_DIRECT
        if (server->rdma) {
@@ -475,7 +486,7 @@ smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
        if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
                wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
 
-       return wsize;
+       return prevent_zero_iosize(wsize, "w");
 }
 
 static unsigned int
@@ -485,13 +496,13 @@ smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
        unsigned int rsize;
 
        /* start with specified rsize, or default */
-       rsize = ctx->rsize ? ctx->rsize : CIFS_DEFAULT_IOSIZE;
+       rsize = ctx->got_rsize ? ctx->vol_rsize : CIFS_DEFAULT_IOSIZE;
        rsize = min_t(unsigned int, rsize, server->max_read);
 
        if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
                rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
 
-       return rsize;
+       return prevent_zero_iosize(rsize, "r");
 }
 
 static unsigned int
@@ -501,7 +512,7 @@ smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
        unsigned int rsize;
 
        /* start with specified rsize, or default */
-       rsize = ctx->rsize ? ctx->rsize : SMB3_DEFAULT_IOSIZE;
+       rsize = ctx->got_rsize ? ctx->vol_rsize : SMB3_DEFAULT_IOSIZE;
        rsize = min_t(unsigned int, rsize, server->max_read);
 #ifdef CONFIG_CIFS_SMB_DIRECT
        if (server->rdma) {
@@ -524,7 +535,7 @@ smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
        if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
                rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
 
-       return rsize;
+       return prevent_zero_iosize(rsize, "r");
 }
 
 /*
index c3ee42188d252ecb851e3c9fa64a7ba34d4f5278..1af827ae757e0d9d0e384306d0abb89925adee46 100644 (file)
@@ -95,6 +95,9 @@
  */
 #define SMB3_DEFAULT_IOSIZE (4 * 1024 * 1024)
 
+/* According to MS-SMB2 specification The minimum recommended value is 65536.*/
+#define CIFS_MIN_DEFAULT_IOSIZE (65536)
+
 /*
  * SMB2 Header Definition
  *