smb: move duplicate definitions to common header file
Subsystems (2)
v7 cover smb: move duplicate definitions to common header file
From: ChenXiaoSong <[email protected]>
In order to maintain the code more easily, move some duplicate definitions to common header file. Add some MS documentation references for macro and struct definitions.
I have tested all patches using xfstests and smbtorture, and no additional test failures were observed in the results. The detailed test results can be found in https://chenxiaosong.com/en/smb-test/20251113.html
v3: https://lore.kernel.org/all/[email protected]/
The following patches from v3 have already been merged into the mainline:
- d877470b5991 smb: move some duplicate definitions to common/cifsglob.h
- 379510a815cb smb/server: fix possible refcount leak in smb2_sess_setup()
- 6fced056d2cc smb/server: fix possible memory leak in smb2_read()
v4: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
The following patches from v4 and v5 have been applied to the ksmbd-for-next-next branch: https://git.samba.org/?p=ksmbd.git;a=shortlog;h=refs/heads/ksmbd-for-next-next
- smb: fix some warnings reported by scripts/checkpatch.pl
- smb: do some cleanups
- smb: move FILE_SYSTEM_SIZE_INFO to common/fscc.h
- smb: move some duplicate struct definitions to common/fscc.h
- smb: move list of FileSystemAttributes to common/fscc.h
- smb: move SMB_NEGOTIATE_REQ to common/smb2pdu.h
- smb: move some duplicate definitions to common/smb2pdu.h
- smb: move create_durable_rsp_v2 to common/smb2pdu.h
- smb: move create_durable_handle_reconnect_v2 to common/smb2pdu.h
- smb: move create_durable_req_v2 to common/smb2pdu.h
- smb: move MAX_CIFS_SMALL_BUFFER_SIZE to common/smbglob.h
- smb/client: fix CAP_BULK_TRANSFER value
- smb: move resume_key_ioctl_rsp to common/smb2pdu.h
- smb: move copychunk definitions to common/smb2pdu.h
- smb: move smb_sockaddr_in and smb_sockaddr_in6 to common/smb2pdu.h
- smb: move SMB1_PROTO_NUMBER to common/smbglob.h
- smb: move get_rfc1002_len() to common/smbglob.h
- smb: move smb_version_values to common/smbglob.h
- smb: rename common/cifsglob.h to common/smbglob.h
v5->v7:
- Patch #0001: use typedef to define both CREATE_DURABLE_REQ and CREATE_DURABLE_RECONN for a single struct.
- Patch #0002: make FILE_SYSTEM_ATTRIBUTE_INFO.FileSystemName a flexible array member.
v6->v7:
- Patch #0001: CREATE_DURABLE -> CREATE_DURABLE_REQ
CREATE_DURABLE_RECONN_REQ -> CREATE_DURABLE_RECONN
v6: https://lore.kernel.org/all/[email protected]/
ChenXiaoSong (2):
smb: move CREATE_DURABLE_RECONN to common/smb2pdu.h
smb: move FILE_SYSTEM_ATTRIBUTE_INFO to common/fscc.h
fs/smb/client/cifspdu.h | 10 ---------- fs/smb/client/smb2pdu.c | 25 ++++++++++++------------- fs/smb/common/fscc.h | 8 ++++++++ fs/smb/common/smb2pdu.h | 4 ++-- fs/smb/server/smb2pdu.c | 12 ++++++------ fs/smb/server/smb2pdu.h | 12 ------------ fs/smb/server/smb_common.h | 7 ------- 7 files changed, 28 insertions(+), 50 deletions(-)
Signature · 1 line
2.43.0
PATCH v7 1/2 smb: move CREATE_DURABLE_RECONN to common/smb2pdu.h
From: ChenXiaoSong <[email protected]>
The fields in struct create_durable_reconn_req and struct create_durable are exactly the same, so remove create_durable_reconn_req from server, and use typedef to define both CREATE_DURABLE_REQ and CREATE_DURABLE_RECONN for a single struct.
Rename the following places:
- struct create_durable -> CREATE_DURABLE_REQ
- struct create_durable_reconn_req -> CREATE_DURABLE_RECONN
The documentation references are:
- SMB2_CREATE_DURABLE_HANDLE_REQUEST in MS-SMB2 2.2.13.2.3
- SMB2_CREATE_DURABLE_HANDLE_RECONNECT in MS-SMB2 2.2.13.2.4
- SMB2_FILEID in MS-SMB2 2.2.14.1
Descriptions of the struct fields:
- __u8 Reserved[16]: DurableRequest field of SMB2_CREATE_DURABLE_HANDLE_REQUEST. A 16-byte field that MUST be reserved.
- __u64 PersistentFileId: Persistent field of 2.2.14.1 SMB2_FILEID
- __u64 VolatileFileId: Volatile field of 2.2.14.1 SMB2_FILEID
- struct Fid: Data field of SMB2_CREATE_DURABLE_HANDLE_RECONNECT.
An SMB2_FILEID structure, as specified in section 2.2.14.1.
--- fs/smb/client/smb2pdu.c | 23 +++++++++++------------ fs/smb/common/smb2pdu.h | 4 ++-- fs/smb/server/smb2pdu.c | 6 +++--- fs/smb/server/smb2pdu.h | 12 ------------ 4 files changed, 16 insertions(+), 29 deletions(-)
fs/smb/client/smb2pdu.c +11 -12
@@ -2229,21 +2229,20 @@ SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)return rc; } - -static struct create_durable * +static CREATE_DURABLE_REQ *create_durable_buf (void ) { -struct create_durable *buf; +CREATE_DURABLE_REQ *buf; - buf =kzalloc (sizeof (struct create_durable ),GFP_KERNEL ); + buf =kzalloc (sizeof (CREATE_DURABLE_REQ ),GFP_KERNEL );if (!buf)return NULL ; buf->ccontext.DataOffset =cpu_to_le16 (offsetof - (struct create_durable , Data)); + (CREATE_DURABLE_REQ , Data)); buf->ccontext.DataLength =cpu_to_le32 (16 ); buf->ccontext.NameOffset =cpu_to_le16 (offsetof - (struct create_durable , Name)); + (CREATE_DURABLE_REQ , Name)); buf->ccontext.NameLength =cpu_to_le16 (4 );/* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */ buf->Name[0 ] ='D' ;
@@ -2253,20 +2252,20 @@ create_durable_buf(void)return buf; } -static struct create_durable * +static CREATE_DURABLE_REQ *create_reconnect_durable_buf (struct cifs_fid *fid) { -struct create_durable *buf; +CREATE_DURABLE_REQ *buf; - buf =kzalloc (sizeof (struct create_durable ),GFP_KERNEL ); + buf =kzalloc (sizeof (CREATE_DURABLE_REQ ),GFP_KERNEL );if (!buf)return NULL ; buf->ccontext.DataOffset =cpu_to_le16 (offsetof - (struct create_durable , Data)); + (CREATE_DURABLE_REQ , Data)); buf->ccontext.DataLength =cpu_to_le32 (16 ); buf->ccontext.NameOffset =cpu_to_le16 (offsetof - (struct create_durable , Name)); + (CREATE_DURABLE_REQ , Name)); buf->ccontext.NameLength =cpu_to_le16 (4 ); buf->Data.Fid.PersistentFileId = fid->persistent_fid; buf->Data.Fid.VolatileFileId = fid->volatile_fid;
@@ -2552,7 +2551,7 @@ add_durable_context(struct kvec *iov, unsigned int *num_iovec, iov[num].iov_base =create_durable_buf ();if (iov[num].iov_base ==NULL )return -ENOMEM ; - iov[num].iov_len =sizeof (struct create_durable ); + iov[num].iov_len =sizeof (CREATE_DURABLE_REQ ); *num_iovec = num +1 ;return 0 ; }
fs/smb/common/smb2pdu.h +2 -2
@@ -1265,7 +1265,7 @@ struct create_posix { } __packed;/* See MS-SMB2 2.2.13.2.3 and MS-SMB2 2.2.13.2.4 */ -struct create_durable { +typedef struct { struct create_context_hdr ccontext; __u8 Name[8]; union {
@@ -1275,7 +1275,7 @@ struct create_durable {__u64 VolatileFileId; } Fid; } Data; -} __packed; +}__packed CREATE_DURABLE_REQ ,CREATE_DURABLE_RECONN ;/* See MS-SMB2 2.2.13.2.5 */ struct create_mxac_req {
fs/smb/server/smb2pdu.c +3 -3
@@ -2766,7 +2766,7 @@ static int parse_durable_handle_context(struct ksmbd_work *work, }case DURABLE_RECONN : { -struct create_durable_reconn_req *recon; +CREATE_DURABLE_RECONN *recon;if (dh_info->type ==DURABLE_RECONN_V2 || dh_info->type ==DURABLE_REQ_V2 ) {
@@ -2776,12 +2776,12 @@ static int parse_durable_handle_context(struct ksmbd_work *work,if (le16_to_cpu (context->DataOffset) +le32_to_cpu (context->DataLength) < -sizeof (struct create_durable_reconn_req )) { +sizeof (CREATE_DURABLE_RECONN )) { err = -EINVAL ; goto out; } - recon = (struct create_durable_reconn_req *)context; + recon = (CREATE_DURABLE_RECONN *)context; persistent_id = recon->Data.Fid.PersistentFileId; dh_info->fp =ksmbd_lookup_durable_fd (persistent_id);if (!dh_info->fp) {
fs/smb/server/smb2pdu.h +0 -12
@@ -68,18 +68,6 @@ struct preauth_integrity_info {#define DURABLE_HANDLE_MAX_TIMEOUT 300000 -struct create_durable_reconn_req { -struct create_context_hdr ccontext; -__u8 Name[8 ]; -union { -__u8 Reserved[16 ]; -struct { -__u64 PersistentFileId; -__u64 VolatileFileId; - } Fid; - } Data; -} __packed; -struct create_alloc_size_req {struct create_context_hdr ccontext;__u8 Name[8 ];
Signature · 1 line
2.43.0
Namjae Jeon
maintainerRe: [PATCH v7 1/2] smb: move CREATE_DURABLE_RECONN to common/smb2pdu.h
Quoted 28 lines · On Thu, Nov 13, 2025 at 10:34 PM <[email protected]> wrote:
From: ChenXiaoSong <[email protected]>
The fields in struct
create_durable_reconn_reqand structcreate_durableare exactly the same, so removecreate_durable_reconn_reqfrom server, and use typedef to define bothCREATE_DURABLE_REQandCREATE_DURABLE_RECONNfor a single struct.Rename the following places:
- struct
create_durable->CREATE_DURABLE_REQ
- structcreate_durable_reconn_req->CREATE_DURABLE_RECONNThe documentation references are:
-
SMB2_CREATE_DURABLE_HANDLE_REQUESTin MS-SMB2 2.2.13.2.3
-SMB2_CREATE_DURABLE_HANDLE_RECONNECTin MS-SMB2 2.2.13.2.4
-SMB2_FILEIDin MS-SMB2 2.2.14.1Descriptions of the struct fields:
-
__u8Reserved[16]: DurableRequest field ofSMB2_CREATE_DURABLE_HANDLE_REQUEST. A 16-byte field that MUST be reserved.
-__u64PersistentFileId: Persistent field of 2.2.14.1SMB2_FILEID
-__u64VolatileFileId: Volatile field of 2.2.14.1SMB2_FILEID
- struct Fid: Data field ofSMB2_CREATE_DURABLE_HANDLE_RECONNECT.
AnSMB2_FILEIDstructure, as specified in section 2.2.14.1.
Quoted 2 lines
Suggested-by Namjae Jeon <[email protected]>Signed-off-by ChenXiaoSong <[email protected]>
I have changed struct names like the following ones and applied it to #ksmbd-for-next-next.
- struct create_durable -> create_durable_req_t
- struct create_durable_reconn_req -> create_durable_reconn_t
Thanks!
PATCH v7 2/2 smb: move FILE_SYSTEM_ATTRIBUTE_INFO to common/fscc.h
In reply to [email protected]From: ChenXiaoSong <[email protected]>
Modify the following places:
- struct filesystem_attribute_info -> FILE_SYSTEM_ATTRIBUTE_INFO
- client: remove MIN_FS_ATTR_INFO_SIZE definition,
MIN_FS_ATTR_INFO_SIZE -> sizeof(FILE_SYSTEM_ATTRIBUTE_INFO)
Then move FILE_SYSTEM_ATTRIBUTE_INFO to common header file.
--- fs/smb/client/cifspdu.h | 10 ---------- fs/smb/client/smb2pdu.c | 2 +- fs/smb/common/fscc.h | 8 ++++++++ fs/smb/server/smb2pdu.c | 6 +++--- fs/smb/server/smb_common.h | 7 ------- 5 files changed, 12 insertions(+), 21 deletions(-)
fs/smb/client/cifspdu.h +0 -10
@@ -2068,16 +2068,6 @@ typedef struct {#define FILE_PORTABLE_DEVICE 0x00004000#define FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL 0x00020000 -/* minimum includes first three fields, and empty FS Name */ -#define MIN_FS_ATTR_INFO_SIZE 12 - -typedef struct { -__le32 Attributes; -__le32 MaxPathNameComponentLength; -__le32 FileSystemNameLen; -char FileSystemName[52 ];/* do not have to save this - get subset? */ -} __attribute__((packed))FILE_SYSTEM_ATTRIBUTE_INFO ; -/******************************************************************************/ /* QueryFileInfo/QueryPathinfo (also for SetPath/SetFile) data buffer formats */ /******************************************************************************/
fs/smb/client/smb2pdu.c +1 -1
@@ -5982,7 +5982,7 @@ SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, min_len =sizeof (FILE_SYSTEM_DEVICE_INFO ); }else if (level ==FS_ATTRIBUTE_INFORMATION ) { max_len =sizeof (FILE_SYSTEM_ATTRIBUTE_INFO ); - min_len =MIN_FS_ATTR_INFO_SIZE ; + min_len =sizeof (FILE_SYSTEM_ATTRIBUTE_INFO ); }else if (level ==FS_SECTOR_SIZE_INFORMATION ) { max_len =sizeof (struct smb3_fs_ss_info ); min_len =sizeof (struct smb3_fs_ss_info );
fs/smb/common/fscc.h +8 -0
@@ -94,6 +94,14 @@ struct smb2_file_network_open_info {__le32 Reserved; } __packed;/* level 34 Query also similar returned in close rsp and open rsp */ +/* See FS-FSCC 2.5.1 */ +typedef struct { +__le32 Attributes; +__le32 MaxPathNameComponentLength; +__le32 FileSystemNameLen; +__le16 FileSystemName[];/* do not have to save this - get subset? */ +} __packed FILE_SYSTEM_ATTRIBUTE_INFO; +/* List of FileSystemAttributes - see MS-FSCC 2.5.1 */ #define FILE_SUPPORTS_SPARSE_VDL 0x10000000/* faster nonsparse extend */ #define FILE_SUPPORTS_BLOCK_REFCOUNTING 0x08000000/* allow ioctl dup extents */
fs/smb/server/smb2pdu.c +3 -3
@@ -5492,10 +5492,10 @@ static int smb2_get_info_filesystem(struct ksmbd_work *work, }case FS_ATTRIBUTE_INFORMATION : { -struct filesystem_attribute_info *info; +FILE_SYSTEM_ATTRIBUTE_INFO *info;size_t sz; - info = (struct filesystem_attribute_info *)rsp->Buffer; + info = (FILE_SYSTEM_ATTRIBUTE_INFO *)rsp->Buffer; info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS | FILE_PERSISTENT_ACLS | FILE_UNICODE_ON_DISK |
@@ -5514,7 +5514,7 @@ static int smb2_get_info_filesystem(struct ksmbd_work *work, "NTFS", PATH_MAX, conn->local_nls, 0); len = len *2 ; info->FileSystemNameLen =cpu_to_le32 (len); - sz =sizeof (struct filesystem_attribute_info ) + len; + sz =sizeof (FILE_SYSTEM_ATTRIBUTE_INFO ) + len; rsp->OutputBufferLength =cpu_to_le32 (sz);break ; }
fs/smb/server/smb_common.h +0 -7
@@ -90,13 +90,6 @@ struct smb_negotiate_rsp {__le16 ByteCount; } __packed; -struct filesystem_attribute_info { -__le32 Attributes; -__le32 MaxPathNameComponentLength; -__le32 FileSystemNameLen; -__le16 FileSystemName[];/* do not have to save this - get subset? */ -} __packed; -struct filesystem_vol_info {__le64 VolumeCreationTime;__le32 SerialNumber;
Signature · 1 line
2.43.0
Namjae Jeon
maintainerRe: [PATCH v7 2/2] smb: move FILE_SYSTEM_ATTRIBUTE_INFO to common/fscc.h
Quoted 47 lines · On Thu, Nov 13, 2025 at 10:34 PM <[email protected]> wrote:
From: ChenXiaoSong <[email protected]>
Modify the following places:
- struct
filesystem_attribute_info->FILE_SYSTEM_ATTRIBUTE_INFO
- client: removeMIN_FS_ATTR_INFO_SIZEdefinition,MIN_FS_ATTR_INFO_SIZE -> sizeof(FILE_SYSTEM_ATTRIBUTE_INFO)Then move
FILE_SYSTEM_ATTRIBUTE_INFOto common header file.Suggested-by Namjae Jeon <[email protected]>Signed-off-by ChenXiaoSong <[email protected]>--- fs/smb/client/cifspdu.h | 10 ---------- fs/smb/client/smb2pdu.c | 2 +- fs/smb/common/fscc.h | 8 ++++++++ fs/smb/server/smb2pdu.c | 6 +++--- fs/smb/server/smb_common.h | 7 ------- 5 files changed, 12 insertions(+), 21 deletions(-)fs/smb/client/cifspdu.h +0
-10@@ -2068,16 +2068,6 @@ typedef struct {#define FILE_PORTABLE_DEVICE 0x00004000#define FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL 0x00020000 -/* minimum includes first three fields, and empty FS Name */ -#define MIN_FS_ATTR_INFO_SIZE 12 - -typedef struct { -__le32 Attributes; -__le32 MaxPathNameComponentLength; -__le32 FileSystemNameLen; -char FileSystemName[52 ];/* do not have to save this - get subset? */ -} __attribute__((packed))FILE_SYSTEM_ATTRIBUTE_INFO ; -/******************************************************************************/ /* QueryFileInfo/QueryPathinfo (also for SetPath/SetFile) data buffer formats */ /******************************************************************************/ fs/smb/client/smb2pdu.c +0
-0
fs/smb/client/smb2pdu.c SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, · line 5985
Quoted 5 lines
fs/smb/client/smb2pdu.c +1
-1@@ -5982,7 +5982,7 @@ SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, min_len =sizeof (FILE_SYSTEM_DEVICE_INFO ); }else if (level ==FS_ATTRIBUTE_INFORMATION ) { max_len =sizeof (FILE_SYSTEM_ATTRIBUTE_INFO ); - min_len =MIN_FS_ATTR_INFO_SIZE ; + min_len =sizeof (FILE_SYSTEM_ATTRIBUTE_INFO );
FILE_SYSTEM_ATTRIBUTE_INFO is being used elsewhere on the smb client, and there are cases where sizeof(FILE_SYSTEM_ATTRIBUTE_INFO) is being used. Will there really be no problems if we change it to flex-array?
Re: [PATCH v7 2/2] smb: move FILE_SYSTEM_ATTRIBUTE_INFO to common/fscc.h
It seems we need to add 52 to max_len in the SMB2_QFS_attr() function, similar to struct smb3_fs_vol_info.
Or we can temporarily use the v5 version for now? v5: https://lore.kernel.org/all/[email protected]/
Thanks,
ChenXiaoSong.
Quoted 56 lines · Namjae Jeon
Quoted 47 lines · On Thu, Nov 13, 2025 at 10:34 PM <[email protected]> wrote:
From: ChenXiaoSong <[email protected]>
Modify the following places:
- struct
filesystem_attribute_info->FILE_SYSTEM_ATTRIBUTE_INFO
- client: removeMIN_FS_ATTR_INFO_SIZEdefinition,MIN_FS_ATTR_INFO_SIZE -> sizeof(FILE_SYSTEM_ATTRIBUTE_INFO)Then move
FILE_SYSTEM_ATTRIBUTE_INFOto common header file.Suggested-by Namjae Jeon <[email protected]>Signed-off-by ChenXiaoSong <[email protected]>--- fs/smb/client/cifspdu.h | 10 ---------- fs/smb/client/smb2pdu.c | 2 +- fs/smb/common/fscc.h | 8 ++++++++ fs/smb/server/smb2pdu.c | 6 +++--- fs/smb/server/smb_common.h | 7 ------- 5 files changed, 12 insertions(+), 21 deletions(-)fs/smb/client/cifspdu.h +0
-10@@ -2068,16 +2068,6 @@ typedef struct {#define FILE_PORTABLE_DEVICE 0x00004000#define FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL 0x00020000 -/* minimum includes first three fields, and empty FS Name */ -#define MIN_FS_ATTR_INFO_SIZE 12 - -typedef struct { -__le32 Attributes; -__le32 MaxPathNameComponentLength; -__le32 FileSystemNameLen; -char FileSystemName[52 ];/* do not have to save this - get subset? */ -} __attribute__((packed))FILE_SYSTEM_ATTRIBUTE_INFO ; -/******************************************************************************/ /* QueryFileInfo/QueryPathinfo (also for SetPath/SetFile) data buffer formats */ /******************************************************************************/ fs/smb/client/smb2pdu.c +0
-0fs/smb/client/smb2pdu.c SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, · line 5985
Quoted 5 lines
fs/smb/client/smb2pdu.c +1
-1@@ -5982,7 +5982,7 @@ SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon, min_len =sizeof (FILE_SYSTEM_DEVICE_INFO ); }else if (level ==FS_ATTRIBUTE_INFORMATION ) { max_len =sizeof (FILE_SYSTEM_ATTRIBUTE_INFO ); - min_len =MIN_FS_ATTR_INFO_SIZE ; + min_len =sizeof (FILE_SYSTEM_ATTRIBUTE_INFO );
FILE_SYSTEM_ATTRIBUTE_INFOis being used elsewhere on the smb client, and there are cases wheresizeof(FILE_SYSTEM_ATTRIBUTE_INFO)is being used. Will there really be no problems if we change it to flex-array?