[linux-cifs-client] [PATCH 4/7] [CIFS] cleanup code by moving often repeated code sequence into a separate function - part1

Jeff Layton jlayton at redhat.com
Fri Jul 25 15:23:17 GMT 2008


From: Günter Kukkukk <linux at kukkukk.com>

move all repeating code sequences, which use
  - cifsConvertToUCS()
to a new function
  - setup_ucs_nls_name()

Signed-off-by: Günter Kukkukk <linux at kukkukk.com>
---

 fs/cifs/cifssmb.c |  278 ++++++++++++-----------------------------------------
 1 files changed, 61 insertions(+), 217 deletions(-)

diff --git a/fs/cifs/cifssmb.c b/fs/cifs/cifssmb.c
index 733862d..12be35f 100644
--- a/fs/cifs/cifssmb.c
+++ b/fs/cifs/cifssmb.c
@@ -444,6 +444,27 @@ static int validate_t2(struct smb_t2_rsp *pSMB)
 		sizeof(struct smb_t2_rsp) + 16);
 	return rc;
 }
+
+static int
+setup_ucs_nls_name(__le16 Flags2, char *dst, const char *src,
+			    const struct nls_table *nls_codepage, int remap)
+{
+	int name_len;
+
+	if (Flags2 & SMBFLG2_UNICODE) {
+		name_len = cifsConvertToUCS((__le16 *)dst, src,
+					    PATH_MAX, nls_codepage, remap);
+		name_len++;	/* trailing null */
+		name_len *= 2;
+	} else { /* BB add path length overrun check */
+		name_len = strnlen(src, PATH_MAX);
+		name_len++;	/* trailing null */
+		strlcpy(dst, src, name_len);
+	}
+
+	return name_len;
+}
+
 int
 CIFSSMBNegotiate(unsigned int xid, struct cifsSesInfo *ses)
 {
@@ -863,17 +884,8 @@ PsxDelete:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
-				     PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else { /* BB add path length overrun check */
-		name_len = strnlen(fileName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, fileName, name_len);
-	}
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      fileName, nls_codepage, remap);
 
 	params = 6 + name_len;
 	pSMB->MaxParameterCount = cpu_to_le16(2);
@@ -935,17 +947,9 @@ DelFileRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->fileName, fileName,
-				     PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {		/* BB improve check for buffer overruns BB */
-		name_len = strnlen(fileName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->fileName, fileName, name_len);
-	}
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->fileName,
+				      fileName, nls_codepage, remap);
+
 	pSMB->SearchAttributes =
 	    cpu_to_le16(ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM);
 	pSMB->BufferFormat = 0x04;
@@ -981,16 +985,8 @@ RmDirRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len = cifsConvertToUCS((__le16 *) pSMB->DirName, dirName,
-					 PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {		/* BB improve check for buffer overruns BB */
-		name_len = strnlen(dirName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->DirName, dirName, name_len);
-	}
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->DirName,
+				      dirName, nls_codepage, remap);
 
 	pSMB->BufferFormat = 0x04;
 	pSMB->hdr.smb_buf_length += name_len + 1;
@@ -1024,16 +1020,8 @@ MkDirRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len = cifsConvertToUCS((__le16 *) pSMB->DirName, name,
-					    PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {		/* BB improve check for buffer overruns BB */
-		name_len = strnlen(name, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->DirName, name, name_len);
-	}
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->DirName, name,
+				      nls_codepage, remap);
 
 	pSMB->BufferFormat = 0x04;
 	pSMB->hdr.smb_buf_length += name_len + 1;
@@ -1072,18 +1060,8 @@ PsxCreat:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->FileName, name,
-				     PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(name, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, name, name_len);
-	}
-
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName, name,
+				      nls_codepage, remap);
 	params = 6 + name_len;
 	count = sizeof(OPEN_PSX_REQ);
 	pSMB->MaxParameterCount = cpu_to_le16(2);
@@ -2274,17 +2252,8 @@ createHardLinkRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len = cifsConvertToUCS((__le16 *) pSMB->FileName, toName,
-					    PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(toName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, toName, name_len);
-	}
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName, toName,
+				      nls_codepage, remap);
 	params = 6 + name_len;
 	pSMB->MaxSetupCount = 0;
 	pSMB->Reserved = 0;
@@ -2296,18 +2265,8 @@ createHardLinkRetry:
 	offset = param_offset + params;
 
 	data_offset = (char *) (&pSMB->hdr.Protocol) + offset;
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len_target =
-		    cifsConvertToUCS((__le16 *) data_offset, fromName, PATH_MAX,
-				     nls_codepage, remap);
-		name_len_target++;	/* trailing null */
-		name_len_target *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len_target = strnlen(fromName, PATH_MAX);
-		name_len_target++;	/* trailing null */
-		strncpy(data_offset, fromName, name_len_target);
-	}
-
+	name_len_target = setup_ucs_nls_name(pSMB->hdr.Flags2, data_offset,
+					     fromName, nls_codepage, remap);
 	pSMB->MaxParameterCount = cpu_to_le16(2);
 	/* BB find exact max on data count below from sess*/
 	pSMB->MaxDataCount = cpu_to_le16(1000);
@@ -2949,17 +2908,8 @@ setAclRetry:
 		      (void **) &pSMBr);
 	if (rc)
 		return rc;
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-			cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
-				      PATH_MAX, nls_codepage, remap);
-		name_len++;     /* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(fileName, PATH_MAX);
-		name_len++;     /* trailing null */
-		strncpy(pSMB->FileName, fileName, name_len);
-	}
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      fileName, nls_codepage, remap);
 	params = 6 + name_len;
 	pSMB->MaxParameterCount = cpu_to_le16(2);
 	/* BB find max SMB size from sess */
@@ -3271,17 +3221,8 @@ QInfRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-			cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
-					PATH_MAX, nls_codepage, remap);
-		name_len++;     /* trailing null */
-		name_len *= 2;
-	} else {
-		name_len = strnlen(searchName, PATH_MAX);
-		name_len++;     /* trailing null */
-		strncpy(pSMB->FileName, searchName, name_len);
-	}
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      searchName, nls_codepage, remap);
 	pSMB->BufferFormat = 0x04;
 	name_len++; /* account for buffer type byte */
 	pSMB->hdr.smb_buf_length += (__u16) name_len;
@@ -3345,18 +3286,8 @@ QPathInfoRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
-				     PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(searchName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, searchName, name_len);
-	}
-
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      searchName, nls_codepage, remap);
 	params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
 	pSMB->TotalDataCount = 0;
 	pSMB->MaxParameterCount = cpu_to_le16(2);
@@ -3445,18 +3376,8 @@ UnixQPathInfoRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
-				  PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(searchName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, searchName, name_len);
-	}
-
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      searchName, nls_codepage, remap);
 	params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
 	pSMB->TotalDataCount = 0;
 	pSMB->MaxParameterCount = cpu_to_le16(2);
@@ -3825,18 +3746,8 @@ GetInodeNumberRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-			cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
-					 PATH_MAX, nls_codepage, remap);
-		name_len++;     /* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(searchName, PATH_MAX);
-		name_len++;     /* trailing null */
-		strncpy(pSMB->FileName, searchName, name_len);
-	}
-
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      searchName, nls_codepage, remap);
 	params = 2 /* level */  + 4 /* rsrvd */  + name_len /* incl null */ ;
 	pSMB->TotalDataCount = 0;
 	pSMB->MaxParameterCount = cpu_to_le16(2);
@@ -4025,19 +3936,11 @@ getDFSRetry:
 	if (ses->capabilities & CAP_DFS)
 		pSMB->hdr.Flags2 |= SMBFLG2_DFS;
 
-	if (ses->capabilities & CAP_UNICODE) {
+	if (ses->capabilities & CAP_UNICODE)
 		pSMB->hdr.Flags2 |= SMBFLG2_UNICODE;
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->RequestFileName,
-				     searchName, PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(searchName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->RequestFileName, searchName, name_len);
-	}
 
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->RequestFileName,
+				      searchName, nls_codepage, remap);
 	if (ses->server) {
 		if (ses->server->secMode &
 		   (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
@@ -4659,17 +4562,8 @@ SetEOFRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
-				     PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(fileName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, fileName, name_len);
-	}
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      fileName, nls_codepage, remap);
 	params = 6 + name_len;
 	data_count = sizeof(struct file_end_of_file_info);
 	pSMB->MaxParameterCount = cpu_to_le16(2);
@@ -4899,18 +4793,8 @@ SetTimesRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
-				     PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(fileName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, fileName, name_len);
-	}
-
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      fileName, nls_codepage, remap);
 	params = 6 + name_len;
 	count = sizeof(FILE_BASIC_INFO);
 	pSMB->MaxParameterCount = cpu_to_le16(2);
@@ -5030,18 +4914,8 @@ setPermsRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
-				     PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(fileName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, fileName, name_len);
-	}
-
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      fileName, nls_codepage, remap);
 	params = 6 + name_len;
 	count = sizeof(FILE_UNIX_BASIC_INFO);
 	pSMB->MaxParameterCount = cpu_to_le16(2);
@@ -5213,18 +5087,8 @@ QAllEAsRetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
-				     PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(searchName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, searchName, name_len);
-	}
-
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      searchName, nls_codepage, remap);
 	params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
 	pSMB->TotalDataCount = 0;
 	pSMB->MaxParameterCount = cpu_to_le16(2);
@@ -5361,18 +5225,8 @@ QEARetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->FileName, searchName,
-				     PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(searchName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, searchName, name_len);
-	}
-
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      searchName, nls_codepage, remap);
 	params = 2 /* level */ + 4 /* reserved */ + name_len /* includes NUL */;
 	pSMB->TotalDataCount = 0;
 	pSMB->MaxParameterCount = cpu_to_le16(2);
@@ -5510,18 +5364,8 @@ SetEARetry:
 	if (rc)
 		return rc;
 
-	if (pSMB->hdr.Flags2 & SMBFLG2_UNICODE) {
-		name_len =
-		    cifsConvertToUCS((__le16 *) pSMB->FileName, fileName,
-				     PATH_MAX, nls_codepage, remap);
-		name_len++;	/* trailing null */
-		name_len *= 2;
-	} else {	/* BB improve the check for buffer overruns BB */
-		name_len = strnlen(fileName, PATH_MAX);
-		name_len++;	/* trailing null */
-		strncpy(pSMB->FileName, fileName, name_len);
-	}
-
+	name_len = setup_ucs_nls_name(pSMB->hdr.Flags2, pSMB->FileName,
+				      fileName, nls_codepage, remap);
 	params = 6 + name_len;
 
 	/* done calculating parms using name_len of file name,



More information about the linux-cifs-client mailing list