Blame SOURCES/0033-exfat-standardize-checksum-calculation.patch

Kmods SIG 50e2b3
From 5875bf287d95314c58add01184f361cc5aa38429 Mon Sep 17 00:00:00 2001
Kmods SIG 50e2b3
From: Tetsuhiro Kohada <kohada.t2@gmail.com>
Kmods SIG 50e2b3
Date: Fri, 29 May 2020 19:14:59 +0900
Kmods SIG 50e2b3
Subject: [Backport 5875bf287d95] exfat: standardize checksum calculation
Kmods SIG 50e2b3
Kmods SIG 50e2b3
To clarify that it is a 16-bit checksum, the parts related to the 16-bit
Kmods SIG 50e2b3
checksum are renamed and change type to u16.
Kmods SIG 50e2b3
Furthermore, replace checksum calculation in exfat_load_upcase_table()
Kmods SIG 50e2b3
with exfat_calc_checksum32().
Kmods SIG 50e2b3
Kmods SIG 50e2b3
Signed-off-by: Tetsuhiro Kohada <kohada.t2@gmail.com>
Kmods SIG 50e2b3
Reviewed-by: Sungjong Seo <sj1557.seo@samsung.com>
Kmods SIG 50e2b3
Signed-off-by: Namjae Jeon <namjae.jeon@samsung.com>
Kmods SIG 50e2b3
---
Kmods SIG 50e2b3
 src/dir.c      | 12 ++++++------
Kmods SIG 50e2b3
 src/exfat_fs.h |  5 ++---
Kmods SIG 50e2b3
 src/misc.c     | 10 ++++------
Kmods SIG 50e2b3
 src/nls.c      | 19 +++++++------------
Kmods SIG 50e2b3
 4 files changed, 19 insertions(+), 27 deletions(-)
Kmods SIG 50e2b3
Kmods SIG 50e2b3
diff --git a/src/dir.c b/src/dir.c
Kmods SIG 50e2b3
index 2902d285bf20412af3a57b0e96a6768e49d6c5ac..de43534aa2997aa87994a9447f95578de9931258 100644
Kmods SIG 50e2b3
--- a/src/dir.c
Kmods SIG 50e2b3
+++ b/src/dir.c
Kmods SIG 50e2b3
@@ -491,7 +491,7 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
Kmods SIG 50e2b3
 	int ret = 0;
Kmods SIG 50e2b3
 	int i, num_entries;
Kmods SIG 50e2b3
 	sector_t sector;
Kmods SIG 50e2b3
-	unsigned short chksum;
Kmods SIG 50e2b3
+	u16 chksum;
Kmods SIG 50e2b3
 	struct exfat_dentry *ep, *fep;
Kmods SIG 50e2b3
 	struct buffer_head *fbh, *bh;
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
@@ -500,7 +500,7 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
Kmods SIG 50e2b3
 		return -EIO;
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 	num_entries = fep->dentry.file.num_ext + 1;
Kmods SIG 50e2b3
-	chksum = exfat_calc_chksum_2byte(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
Kmods SIG 50e2b3
+	chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 	for (i = 1; i < num_entries; i++) {
Kmods SIG 50e2b3
 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL);
Kmods SIG 50e2b3
@@ -508,7 +508,7 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
Kmods SIG 50e2b3
 			ret = -EIO;
Kmods SIG 50e2b3
 			goto release_fbh;
Kmods SIG 50e2b3
 		}
Kmods SIG 50e2b3
-		chksum = exfat_calc_chksum_2byte(ep, DENTRY_SIZE, chksum,
Kmods SIG 50e2b3
+		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
Kmods SIG 50e2b3
 				CS_DEFAULT);
Kmods SIG 50e2b3
 		brelse(bh);
Kmods SIG 50e2b3
 	}
Kmods SIG 50e2b3
@@ -593,8 +593,8 @@ void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 	for (i = 0; i < es->num_entries; i++) {
Kmods SIG 50e2b3
 		ep = exfat_get_dentry_cached(es, i);
Kmods SIG 50e2b3
-		chksum = exfat_calc_chksum_2byte(ep, DENTRY_SIZE, chksum,
Kmods SIG 50e2b3
-						 chksum_type);
Kmods SIG 50e2b3
+		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
Kmods SIG 50e2b3
+					     chksum_type);
Kmods SIG 50e2b3
 		chksum_type = CS_DEFAULT;
Kmods SIG 50e2b3
 	}
Kmods SIG 50e2b3
 	ep = exfat_get_dentry_cached(es, 0);
Kmods SIG 50e2b3
@@ -1000,7 +1000,7 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
Kmods SIG 50e2b3
 			}
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 			if (entry_type == TYPE_STREAM) {
Kmods SIG 50e2b3
-				unsigned short name_hash;
Kmods SIG 50e2b3
+				u16 name_hash;
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 				if (step != DIRENT_STEP_STRM) {
Kmods SIG 50e2b3
 					step = DIRENT_STEP_FILE;
Kmods SIG 50e2b3
diff --git a/src/exfat_fs.h b/src/exfat_fs.h
Kmods SIG 50e2b3
index 8c2a70bfaa10d4743c46aad74254e363d4079c74..595f3117f4924893a07eba48b55ef3d7d6edbee6 100644
Kmods SIG 50e2b3
--- a/src/exfat_fs.h
Kmods SIG 50e2b3
+++ b/src/exfat_fs.h
Kmods SIG 50e2b3
@@ -137,7 +137,7 @@ struct exfat_dentry_namebuf {
Kmods SIG 50e2b3
 struct exfat_uni_name {
Kmods SIG 50e2b3
 	/* +3 for null and for converting */
Kmods SIG 50e2b3
 	unsigned short name[MAX_NAME_LENGTH + 3];
Kmods SIG 50e2b3
-	unsigned short name_hash;
Kmods SIG 50e2b3
+	u16 name_hash;
Kmods SIG 50e2b3
 	unsigned char name_len;
Kmods SIG 50e2b3
 };
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
@@ -512,8 +512,7 @@ void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
Kmods SIG 50e2b3
 void exfat_truncate_atime(struct timespec64 *ts);
Kmods SIG 50e2b3
 void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
Kmods SIG 50e2b3
 		u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);
Kmods SIG 50e2b3
-unsigned short exfat_calc_chksum_2byte(void *data, int len,
Kmods SIG 50e2b3
-		unsigned short chksum, int type);
Kmods SIG 50e2b3
+u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type);
Kmods SIG 50e2b3
 u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);
Kmods SIG 50e2b3
 void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync);
Kmods SIG 50e2b3
 void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
Kmods SIG 50e2b3
diff --git a/src/misc.c b/src/misc.c
Kmods SIG 50e2b3
index b82d2dd5bd7cb291ffe5ed5091c9214593c32a69..17d41f3d3709287f6654dd52bd540e3f60fee285 100644
Kmods SIG 50e2b3
--- a/src/misc.c
Kmods SIG 50e2b3
+++ b/src/misc.c
Kmods SIG 50e2b3
@@ -136,17 +136,15 @@ void exfat_truncate_atime(struct timespec64 *ts)
Kmods SIG 50e2b3
 	ts->tv_nsec = 0;
Kmods SIG 50e2b3
 }
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
-unsigned short exfat_calc_chksum_2byte(void *data, int len,
Kmods SIG 50e2b3
-		unsigned short chksum, int type)
Kmods SIG 50e2b3
+u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type)
Kmods SIG 50e2b3
 {
Kmods SIG 50e2b3
 	int i;
Kmods SIG 50e2b3
-	unsigned char *c = (unsigned char *)data;
Kmods SIG 50e2b3
+	u8 *c = (u8 *)data;
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 	for (i = 0; i < len; i++, c++) {
Kmods SIG 50e2b3
-		if (((i == 2) || (i == 3)) && (type == CS_DIR_ENTRY))
Kmods SIG 50e2b3
+		if (unlikely(type == CS_DIR_ENTRY && (i == 2 || i == 3)))
Kmods SIG 50e2b3
 			continue;
Kmods SIG 50e2b3
-		chksum = (((chksum & 1) << 15) | ((chksum & 0xFFFE) >> 1)) +
Kmods SIG 50e2b3
-			(unsigned short)*c;
Kmods SIG 50e2b3
+		chksum = ((chksum << 15) | (chksum >> 1)) + *c;
Kmods SIG 50e2b3
 	}
Kmods SIG 50e2b3
 	return chksum;
Kmods SIG 50e2b3
 }
Kmods SIG 50e2b3
diff --git a/src/nls.c b/src/nls.c
Kmods SIG 50e2b3
index 1ebda90cbdd7c81c507f662332a53eb52bf92606..19321773dd078a42f8c9659e15032502b33bddcd 100644
Kmods SIG 50e2b3
--- a/src/nls.c
Kmods SIG 50e2b3
+++ b/src/nls.c
Kmods SIG 50e2b3
@@ -527,7 +527,7 @@ static int exfat_utf8_to_utf16(struct super_block *sb,
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 	*uniname = '\0';
Kmods SIG 50e2b3
 	p_uniname->name_len = unilen;
Kmods SIG 50e2b3
-	p_uniname->name_hash = exfat_calc_chksum_2byte(upname, unilen << 1, 0,
Kmods SIG 50e2b3
+	p_uniname->name_hash = exfat_calc_chksum16(upname, unilen << 1, 0,
Kmods SIG 50e2b3
 			CS_DEFAULT);
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 	if (p_lossy)
Kmods SIG 50e2b3
@@ -623,7 +623,7 @@ static int exfat_nls_to_ucs2(struct super_block *sb,
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 	*uniname = '\0';
Kmods SIG 50e2b3
 	p_uniname->name_len = unilen;
Kmods SIG 50e2b3
-	p_uniname->name_hash = exfat_calc_chksum_2byte(upname, unilen << 1, 0,
Kmods SIG 50e2b3
+	p_uniname->name_hash = exfat_calc_chksum16(upname, unilen << 1, 0,
Kmods SIG 50e2b3
 			CS_DEFAULT);
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 	if (p_lossy)
Kmods SIG 50e2b3
@@ -655,7 +655,8 @@ static int exfat_load_upcase_table(struct super_block *sb,
Kmods SIG 50e2b3
 {
Kmods SIG 50e2b3
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
Kmods SIG 50e2b3
 	unsigned int sect_size = sb->s_blocksize;
Kmods SIG 50e2b3
-	unsigned int i, index = 0, checksum = 0;
Kmods SIG 50e2b3
+	unsigned int i, index = 0;
Kmods SIG 50e2b3
+	u32 chksum = 0;
Kmods SIG 50e2b3
 	int ret;
Kmods SIG 50e2b3
 	unsigned char skip = false;
Kmods SIG 50e2b3
 	unsigned short *upcase_table;
Kmods SIG 50e2b3
@@ -681,13 +682,6 @@ static int exfat_load_upcase_table(struct super_block *sb,
Kmods SIG 50e2b3
 		for (i = 0; i < sect_size && index <= 0xFFFF; i += 2) {
Kmods SIG 50e2b3
 			unsigned short uni = get_unaligned_le16(bh->b_data + i);
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
-			checksum = ((checksum & 1) ? 0x80000000 : 0) +
Kmods SIG 50e2b3
-				(checksum >> 1) +
Kmods SIG 50e2b3
-				*(((unsigned char *)bh->b_data) + i);
Kmods SIG 50e2b3
-			checksum = ((checksum & 1) ? 0x80000000 : 0) +
Kmods SIG 50e2b3
-				(checksum >> 1) +
Kmods SIG 50e2b3
-				*(((unsigned char *)bh->b_data) + (i + 1));
Kmods SIG 50e2b3
-
Kmods SIG 50e2b3
 			if (skip) {
Kmods SIG 50e2b3
 				index += uni;
Kmods SIG 50e2b3
 				skip = false;
Kmods SIG 50e2b3
@@ -701,13 +695,14 @@ static int exfat_load_upcase_table(struct super_block *sb,
Kmods SIG 50e2b3
 			}
Kmods SIG 50e2b3
 		}
Kmods SIG 50e2b3
 		brelse(bh);
Kmods SIG 50e2b3
+		chksum = exfat_calc_chksum32(bh->b_data, i, chksum, CS_DEFAULT);
Kmods SIG 50e2b3
 	}
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
-	if (index >= 0xFFFF && utbl_checksum == checksum)
Kmods SIG 50e2b3
+	if (index >= 0xFFFF && utbl_checksum == chksum)
Kmods SIG 50e2b3
 		return 0;
Kmods SIG 50e2b3
 
Kmods SIG 50e2b3
 	exfat_err(sb, "failed to load upcase table (idx : 0x%08x, chksum : 0x%08x, utbl_chksum : 0x%08x)",
Kmods SIG 50e2b3
-		  index, checksum, utbl_checksum);
Kmods SIG 50e2b3
+		  index, chksum, utbl_checksum);
Kmods SIG 50e2b3
 	ret = -EINVAL;
Kmods SIG 50e2b3
 free_table:
Kmods SIG 50e2b3
 	exfat_free_upcase_table(sbi);
Kmods SIG 50e2b3
-- 
Kmods SIG 50e2b3
2.31.1
Kmods SIG 50e2b3