]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
cifs: SMB1 split: misc.c
authorDavid Howells <dhowells@redhat.com>
Wed, 17 Dec 2025 15:22:19 +0000 (15:22 +0000)
committerSteve French <stfrench@microsoft.com>
Sun, 8 Feb 2026 23:07:45 +0000 (17:07 -0600)
Split SMB1 bits from misc.c to smb1misc.c.

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Steve French <sfrench@samba.org>
cc: Paulo Alcantara <pc@manguebit.org>
cc: Enzo Matsumiya <ematsumiya@suse.de>
cc: linux-cifs@vger.kernel.org
cc: linux-fsdevel@vger.kernel.org
cc: linux-kernel@vger.kernel.org
Acked-by: Enzo Matsumiya <ematsumiya@suse.de>
Signed-off-by: Steve French <stfrench@microsoft.com>
fs/smb/client/Makefile
fs/smb/client/cifsproto.h
fs/smb/client/misc.c
fs/smb/client/smb1misc.c [new file with mode: 0644]
fs/smb/client/smb1proto.h

index c51f67a5caaa150a43d9357400452231a240c84d..5288f9de07cdbf4f8eaf12c37f3d6175e766279d 100644 (file)
@@ -35,6 +35,7 @@ cifs-$(CONFIG_CIFS_ROOT) += cifsroot.o
 cifs-$(CONFIG_CIFS_ALLOW_INSECURE_LEGACY) += \
        cifssmb.o \
        smb1debug.o \
+       smb1misc.o \
        smb1ops.o \
        smb1transport.o
 
index 6cf084aeb30e1e73e01e6468bf1740b04340baba..fc97c6c452d26dbe361868992b98521a1aa59841 100644 (file)
@@ -131,7 +131,6 @@ void cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
 void cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
                                           bool mark_smb_session);
 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session);
-bool is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv);
 bool backup_cred(struct cifs_sb_info *cifs_sb);
 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file,
                            bool from_readdir);
@@ -157,9 +156,6 @@ void cifs_set_port(struct sockaddr *addr, const unsigned short int port);
 int map_smb_to_linux_error(char *buf, bool logErr);
 int map_and_check_smb_error(struct TCP_Server_Info *server,
                            struct mid_q_entry *mid, bool logErr);
-unsigned int header_assemble(struct smb_hdr *buffer, char smb_command,
-                            const struct cifs_tcon *treeCon, int word_count
-                            /* length of fixed section (word count) in two byte units  */);
 int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses,
                   struct TCP_Server_Info *server,
                   const struct nls_table *nls_cp);
index 273c54d398576169683fa6cde8b7c9e6e1ce9842..1773e3b471aaa1ce70bd36bd1065afad6e2f10ea 100644 (file)
@@ -262,171 +262,6 @@ free_rsp_buf(int resp_buftype, void *rsp)
                cifs_buf_release(rsp);
 }
 
-/* NB: MID can not be set if treeCon not passed in, in that
-   case it is responsibility of caller to set the mid */
-unsigned int
-header_assemble(struct smb_hdr *buffer, char smb_command,
-               const struct cifs_tcon *treeCon, int word_count
-               /* length of fixed section (word count) in two byte units  */)
-{
-       unsigned int in_len;
-       char *temp = (char *) buffer;
-
-       memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
-
-       in_len = (2 * word_count) + sizeof(struct smb_hdr) +
-               2 /* for bcc field itself */;
-
-       buffer->Protocol[0] = 0xFF;
-       buffer->Protocol[1] = 'S';
-       buffer->Protocol[2] = 'M';
-       buffer->Protocol[3] = 'B';
-       buffer->Command = smb_command;
-       buffer->Flags = 0x00;   /* case sensitive */
-       buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
-       buffer->Pid = cpu_to_le16((__u16)current->tgid);
-       buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
-       if (treeCon) {
-               buffer->Tid = treeCon->tid;
-               if (treeCon->ses) {
-                       if (treeCon->ses->capabilities & CAP_UNICODE)
-                               buffer->Flags2 |= SMBFLG2_UNICODE;
-                       if (treeCon->ses->capabilities & CAP_STATUS32)
-                               buffer->Flags2 |= SMBFLG2_ERR_STATUS;
-
-                       /* Uid is not converted */
-                       buffer->Uid = treeCon->ses->Suid;
-                       if (treeCon->ses->server)
-                               buffer->Mid = get_next_mid(treeCon->ses->server);
-               }
-               if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
-                       buffer->Flags2 |= SMBFLG2_DFS;
-               if (treeCon->nocase)
-                       buffer->Flags  |= SMBFLG_CASELESS;
-               if ((treeCon->ses) && (treeCon->ses->server))
-                       if (treeCon->ses->server->sign)
-                               buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
-       }
-
-/*  endian conversion of flags is now done just before sending */
-       buffer->WordCount = (char) word_count;
-       return in_len;
-}
-
-bool
-is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
-{
-       struct smb_hdr *buf = (struct smb_hdr *)buffer;
-       struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
-       struct TCP_Server_Info *pserver;
-       struct cifs_ses *ses;
-       struct cifs_tcon *tcon;
-       struct cifsInodeInfo *pCifsInode;
-       struct cifsFileInfo *netfile;
-
-       cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
-       if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
-          (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
-               struct smb_com_transaction_change_notify_rsp *pSMBr =
-                       (struct smb_com_transaction_change_notify_rsp *)buf;
-               struct file_notify_information *pnotify;
-               __u32 data_offset = 0;
-               size_t len = srv->total_read - srv->pdu_size;
-
-               if (get_bcc(buf) > sizeof(struct file_notify_information)) {
-                       data_offset = le32_to_cpu(pSMBr->DataOffset);
-
-                       if (data_offset >
-                           len - sizeof(struct file_notify_information)) {
-                               cifs_dbg(FYI, "Invalid data_offset %u\n",
-                                        data_offset);
-                               return true;
-                       }
-                       pnotify = (struct file_notify_information *)
-                               ((char *)&pSMBr->hdr.Protocol + data_offset);
-                       cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
-                                pnotify->FileName, pnotify->Action);
-                       /*   cifs_dump_mem("Rcvd notify Data: ",buf,
-                               sizeof(struct smb_hdr)+60); */
-                       return true;
-               }
-               if (pSMBr->hdr.Status.CifsError) {
-                       cifs_dbg(FYI, "notify err 0x%x\n",
-                                pSMBr->hdr.Status.CifsError);
-                       return true;
-               }
-               return false;
-       }
-       if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
-               return false;
-       if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
-               /* no sense logging error on invalid handle on oplock
-                  break - harmless race between close request and oplock
-                  break response is expected from time to time writing out
-                  large dirty files cached on the client */
-               if ((NT_STATUS_INVALID_HANDLE) ==
-                  le32_to_cpu(pSMB->hdr.Status.CifsError)) {
-                       cifs_dbg(FYI, "Invalid handle on oplock break\n");
-                       return true;
-               } else if (ERRbadfid ==
-                  le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
-                       return true;
-               } else {
-                       return false; /* on valid oplock brk we get "request" */
-               }
-       }
-       if (pSMB->hdr.WordCount != 8)
-               return false;
-
-       cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
-                pSMB->LockType, pSMB->OplockLevel);
-       if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
-               return false;
-
-       /* If server is a channel, select the primary channel */
-       pserver = SERVER_IS_CHAN(srv) ? srv->primary_server : srv;
-
-       /* look up tcon based on tid & uid */
-       spin_lock(&cifs_tcp_ses_lock);
-       list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
-               if (cifs_ses_exiting(ses))
-                       continue;
-               list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
-                       if (tcon->tid != buf->Tid)
-                               continue;
-
-                       cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
-                       spin_lock(&tcon->open_file_lock);
-                       list_for_each_entry(netfile, &tcon->openFileList, tlist) {
-                               if (pSMB->Fid != netfile->fid.netfid)
-                                       continue;
-
-                               cifs_dbg(FYI, "file id match, oplock break\n");
-                               pCifsInode = CIFS_I(d_inode(netfile->dentry));
-
-                               set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
-                                       &pCifsInode->flags);
-
-                               netfile->oplock_epoch = 0;
-                               netfile->oplock_level = pSMB->OplockLevel;
-                               netfile->oplock_break_cancelled = false;
-                               cifs_queue_oplock_break(netfile);
-
-                               spin_unlock(&tcon->open_file_lock);
-                               spin_unlock(&cifs_tcp_ses_lock);
-                               return true;
-                       }
-                       spin_unlock(&tcon->open_file_lock);
-                       spin_unlock(&cifs_tcp_ses_lock);
-                       cifs_dbg(FYI, "No matching file for oplock break\n");
-                       return true;
-               }
-       }
-       spin_unlock(&cifs_tcp_ses_lock);
-       cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
-       return true;
-}
-
 void
 dump_smb(void *buf, int smb_buf_length)
 {
diff --git a/fs/smb/client/smb1misc.c b/fs/smb/client/smb1misc.c
new file mode 100644 (file)
index 0000000..d73ef87
--- /dev/null
@@ -0,0 +1,177 @@
+// SPDX-License-Identifier: LGPL-2.1
+/*
+ *
+ *   Copyright (C) International Business Machines  Corp., 2002,2008
+ *   Author(s): Steve French (sfrench@us.ibm.com)
+ *
+ */
+
+#include "smb1proto.h"
+#include "smberr.h"
+#include "nterr.h"
+#include "cifs_debug.h"
+
+/* NB: MID can not be set if treeCon not passed in, in that
+   case it is responsibility of caller to set the mid */
+unsigned int
+header_assemble(struct smb_hdr *buffer, char smb_command,
+               const struct cifs_tcon *treeCon, int word_count
+               /* length of fixed section (word count) in two byte units  */)
+{
+       unsigned int in_len;
+       char *temp = (char *) buffer;
+
+       memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
+
+       in_len = (2 * word_count) + sizeof(struct smb_hdr) +
+               2 /* for bcc field itself */;
+
+       buffer->Protocol[0] = 0xFF;
+       buffer->Protocol[1] = 'S';
+       buffer->Protocol[2] = 'M';
+       buffer->Protocol[3] = 'B';
+       buffer->Command = smb_command;
+       buffer->Flags = 0x00;   /* case sensitive */
+       buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
+       buffer->Pid = cpu_to_le16((__u16)current->tgid);
+       buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
+       if (treeCon) {
+               buffer->Tid = treeCon->tid;
+               if (treeCon->ses) {
+                       if (treeCon->ses->capabilities & CAP_UNICODE)
+                               buffer->Flags2 |= SMBFLG2_UNICODE;
+                       if (treeCon->ses->capabilities & CAP_STATUS32)
+                               buffer->Flags2 |= SMBFLG2_ERR_STATUS;
+
+                       /* Uid is not converted */
+                       buffer->Uid = treeCon->ses->Suid;
+                       if (treeCon->ses->server)
+                               buffer->Mid = get_next_mid(treeCon->ses->server);
+               }
+               if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
+                       buffer->Flags2 |= SMBFLG2_DFS;
+               if (treeCon->nocase)
+                       buffer->Flags  |= SMBFLG_CASELESS;
+               if ((treeCon->ses) && (treeCon->ses->server))
+                       if (treeCon->ses->server->sign)
+                               buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
+       }
+
+/*  endian conversion of flags is now done just before sending */
+       buffer->WordCount = (char) word_count;
+       return in_len;
+}
+
+bool
+is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
+{
+       struct smb_hdr *buf = (struct smb_hdr *)buffer;
+       struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
+       struct TCP_Server_Info *pserver;
+       struct cifs_ses *ses;
+       struct cifs_tcon *tcon;
+       struct cifsInodeInfo *pCifsInode;
+       struct cifsFileInfo *netfile;
+
+       cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
+       if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
+          (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
+               struct smb_com_transaction_change_notify_rsp *pSMBr =
+                       (struct smb_com_transaction_change_notify_rsp *)buf;
+               struct file_notify_information *pnotify;
+               __u32 data_offset = 0;
+               size_t len = srv->total_read - srv->pdu_size;
+
+               if (get_bcc(buf) > sizeof(struct file_notify_information)) {
+                       data_offset = le32_to_cpu(pSMBr->DataOffset);
+
+                       if (data_offset >
+                           len - sizeof(struct file_notify_information)) {
+                               cifs_dbg(FYI, "Invalid data_offset %u\n",
+                                        data_offset);
+                               return true;
+                       }
+                       pnotify = (struct file_notify_information *)
+                               ((char *)&pSMBr->hdr.Protocol + data_offset);
+                       cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
+                                pnotify->FileName, pnotify->Action);
+                       /*   cifs_dump_mem("Rcvd notify Data: ",buf,
+                               sizeof(struct smb_hdr)+60); */
+                       return true;
+               }
+               if (pSMBr->hdr.Status.CifsError) {
+                       cifs_dbg(FYI, "notify err 0x%x\n",
+                                pSMBr->hdr.Status.CifsError);
+                       return true;
+               }
+               return false;
+       }
+       if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
+               return false;
+       if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
+               /* no sense logging error on invalid handle on oplock
+                  break - harmless race between close request and oplock
+                  break response is expected from time to time writing out
+                  large dirty files cached on the client */
+               if ((NT_STATUS_INVALID_HANDLE) ==
+                  le32_to_cpu(pSMB->hdr.Status.CifsError)) {
+                       cifs_dbg(FYI, "Invalid handle on oplock break\n");
+                       return true;
+               } else if (ERRbadfid ==
+                  le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
+                       return true;
+               } else {
+                       return false; /* on valid oplock brk we get "request" */
+               }
+       }
+       if (pSMB->hdr.WordCount != 8)
+               return false;
+
+       cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
+                pSMB->LockType, pSMB->OplockLevel);
+       if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
+               return false;
+
+       /* If server is a channel, select the primary channel */
+       pserver = SERVER_IS_CHAN(srv) ? srv->primary_server : srv;
+
+       /* look up tcon based on tid & uid */
+       spin_lock(&cifs_tcp_ses_lock);
+       list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
+               if (cifs_ses_exiting(ses))
+                       continue;
+               list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
+                       if (tcon->tid != buf->Tid)
+                               continue;
+
+                       cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
+                       spin_lock(&tcon->open_file_lock);
+                       list_for_each_entry(netfile, &tcon->openFileList, tlist) {
+                               if (pSMB->Fid != netfile->fid.netfid)
+                                       continue;
+
+                               cifs_dbg(FYI, "file id match, oplock break\n");
+                               pCifsInode = CIFS_I(d_inode(netfile->dentry));
+
+                               set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
+                                       &pCifsInode->flags);
+
+                               netfile->oplock_epoch = 0;
+                               netfile->oplock_level = pSMB->OplockLevel;
+                               netfile->oplock_break_cancelled = false;
+                               cifs_queue_oplock_break(netfile);
+
+                               spin_unlock(&tcon->open_file_lock);
+                               spin_unlock(&cifs_tcp_ses_lock);
+                               return true;
+                       }
+                       spin_unlock(&tcon->open_file_lock);
+                       spin_unlock(&cifs_tcp_ses_lock);
+                       cifs_dbg(FYI, "No matching file for oplock break\n");
+                       return true;
+               }
+       }
+       spin_unlock(&cifs_tcp_ses_lock);
+       cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
+       return true;
+}
index 24aefcbf96af49d2e38ba5fe636d0b8d1d242825..08bf6cc157fb34f3c123466891fdba75b633c507 100644 (file)
@@ -219,6 +219,13 @@ int CIFSSMBSetEA(const unsigned int xid, struct cifs_tcon *tcon,
 void cifs_dump_detail(void *buf, size_t buf_len,
                      struct TCP_Server_Info *server);
 
+/*
+ * smb1misc.c
+ */
+unsigned int header_assemble(struct smb_hdr *buffer, char smb_command,
+                            const struct cifs_tcon *treeCon, int word_count);
+bool is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv);
+
 /*
  * smb1ops.c
  */