Skip to contentSkip to conversation

exfat: redefine PBR as boot_sector

PATCH Superseded Tetsuhiro Kohada 5 participants 17 messages May 25, 2020 – May 29, 2020

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 exfat: redefine PBR as boot_sector

Aggregate PBR related definitions and redefine as "boot_sector" to comply with the exFAT specification.
And, rename variable names including 'pbr'.

---
 fs/exfat/exfat_fs.h  |  2 +-
 fs/exfat/exfat_raw.h | 79 +++++++++++++++--------------------------
 fs/exfat/super.c     | 84 ++++++++++++++++++++++----------------------
 3 files changed, 72 insertions(+), 93 deletions(-)
fs/exfat/exfat_fs.h +1 -1
index 294aa7792bc3..b0e5b9afc56c 100644
@@ -231,7 +231,7 @@ struct exfat_sb_info {
 	unsigned int root_dir; /* root dir cluster */
 	unsigned int dentries_per_clu; /* num of dentries per cluster */
 	unsigned int vol_flag; /* volume dirty flag */
-	struct buffer_head *pbr_bh; /* buffer_head of PBR sector */
+	struct buffer_head *boot_bh; /* buffer_head of BOOT sector */
 
 	unsigned int map_clu; /* allocation bitmap start cluster */
 	unsigned int map_sectors; /* num of allocation bitmap sectors */
fs/exfat/exfat_raw.h +29 -50

100 lines not on this page · raw message

fs/exfat/super.c +42 -42

201 lines not on this page · raw message

Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/4 exfat: separate the boot sector analysis

Separate the boot sector analysis to read_boot_sector(). Furthermore, add a strict consistency check, because overlapping areas can cause serious corruption.

---
 fs/exfat/exfat_raw.h |  1 +
 fs/exfat/super.c     | 96 +++++++++++++++++++++++---------------------
 2 files changed, 52 insertions(+), 45 deletions(-)
fs/exfat/exfat_raw.h +1 -0
index b373dc4e099f..65f884785192 100644
@@ -15,6 +15,7 @@
 
 #define VOL_CLEAN		0x0000
 #define VOL_DIRTY		0x0002
+#define ERR_MEDIUM		0x0004
 
 #define EXFAT_EOF_CLUSTER	0xFFFFFFFFu
 #define EXFAT_BAD_CLUSTER	0xFFFFFFF7u
fs/exfat/super.c +51 -45

166 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

PATCH 3/4 exfat: add boot region verification

In reply to Tetsuhiro Kohada

Add Boot-Regions verification specified in exFAT specification. Note that the checksum type is strongly related to the raw structure, so the'u32 'type is used to clarify the number of bits.

---
 fs/exfat/exfat_fs.h |  1 +
 fs/exfat/misc.c     | 14 +++++++++++++
 fs/exfat/super.c    | 50 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+)
fs/exfat/exfat_fs.h +1 -0
index b0e5b9afc56c..15817281b3c8 100644
@@ -517,6 +517,7 @@ void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
 		u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);
 unsigned short exfat_calc_chksum_2byte(void *data, int len,
 		unsigned short chksum, int type);
+u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);
 void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync);
 void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
 		unsigned int size, unsigned char flags);
fs/exfat/misc.c +14 -0
index ab7f88b1f6d3..b82d2dd5bd7c 100644
@@ -151,6 +151,20 @@ unsigned short exfat_calc_chksum_2byte(void *data, int len,
 	return chksum;
 }
 
+u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type)
+{
+	int i;
+	u8 *c = (u8 *)data;
+
+	for (i = 0; i < len; i++, c++) {
+		if (unlikely(type == CS_BOOT_SECTOR &&
+			     (i == 106 || i == 107 || i == 112)))
+			continue;
+		chksum = ((chksum << 31) | (chksum >> 1)) + *c;
+	}
+	return chksum;
+}
+
 void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync)
 {
 	set_bit(EXFAT_SB_DIRTY, &EXFAT_SB(sb)->s_state);
fs/exfat/super.c +50 -0

62 lines not on this page · raw message

Signature · 1 line
2.25.1
#3 lore raw

RE: [PATCH 3/4] exfat: add boot region verification

[snip]

Quoted 6 lines
fs/exfat/super.c +6 -0
+static int exfat_verify_boot_region(struct super_block *sb) {
+	struct buffer_head *bh = NULL;
+	u32 chksum = 0, *p_sig, *p_chksum;
+	int sn, i;
+
+	/* read boot sector sub-regions */

fs/exfat/super.c static int exfat_read_boot_sector(struct super_block *sb) · line 505

Quoted 10 lines
fs/exfat/super.c +10 -0
+	for (sn = 0; sn < 11; sn++) {
+		bh = sb_bread(sb, sn);
+		if (!bh)
+			return -EIO;
+
+		if (sn != 0 && sn <= 8) {
+			/* extended boot sector sub-regions */
+			p_sig = (u32 *)&bh->b_data[sb->s_blocksize - 4];
+			if (le32_to_cpu(*p_sig) != EXBOOT_SIGNATURE) {
+				exfat_err(sb, "no exboot-signature");
                                exfat_warn(sb, "Invalid exboot-signature(sector = %d): 0x%08x", sn, *p_sig);

Review

Quoted 2 lines
(quoted diff) +2 -0
+				brelse(bh);
+				return -EINVAL;

Don't make mount error, Just print warning message.

Quoted 7 lines
fs/exfat/super.c +7 -0
+			}
+		}
+
+		chksum = exfat_calc_chksum32(bh->b_data, sb->s_blocksize,
+			chksum, sn ? CS_DEFAULT : CS_BOOT_SECTOR);
+		brelse(bh);
+	}

fs/exfat/super.c static int exfat_read_boot_sector(struct super_block *sb) · line 524

Quoted 10 lines
fs/exfat/super.c +10 -0
+
+	/* boot checksum sub-regions */
+	bh = sb_bread(sb, sn);
+	if (!bh)
+		return -EIO;
+
+	for (i = 0; i < sb->s_blocksize; i += sizeof(u32)) {
+		p_chksum = (u32 *)&bh->b_data[i];
+		if (le32_to_cpu(*p_chksum) != chksum) {
+			exfat_err(sb, "mismatch checksum");

Print invalid checksum value also.

Quoted 25 lines
fs/exfat/super.c +8 -0
+			brelse(bh);
+			return -EINVAL;
+		}
+	}
+	brelse(bh);
+	return 0;
+}
+
 /* mount the file system volume */
 static int __exfat_fill_super(struct super_block *sb)  { @@ -498,6 +542,12 @@ static int

__exfat_fill_super(struct super_block *sb)

fs/exfat/super.c +6 -1
 		goto free_bh;
 	}
 
+	ret = exfat_verify_boot_region(sb);
+	if (ret) {
+		exfat_err(sb, "invalid boot region");
+		goto free_bh;
+	}
+
 	ret = exfat_create_upcase_table(sb);
 	if (ret) {
 		exfat_err(sb, "failed to load upcase table");
--

2.25.1

#4 lore raw

Re: [PATCH 3/4] exfat: add boot region verification

In reply to Tetsuhiro Kohada
Thank you for the patch! Perhaps something to improve:

Hi Tetsuhiro,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on next-20200519]
[cannot apply to linus/master v5.7-rc7 v5.7-rc6 v5.7-rc5 v5.7-rc7] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Tetsuhiro-Kohada/exfat-redefine-PBR-as-boot_sector/20200525-195329
base:    fb57b1fabcb28f358901b2df90abd2b48abc1ca8
config: c6x-randconfig-s032-20200529 (attached as .config)
compiler: c6x-elf-gcc (GCC) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.1-243-gc100a7ab-dirty
        # save the attached .config to linux build tree
        make W=1 C=1 ARCH=c6x CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__'

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)

>> fs/exfat/super.c:485:29: sparse: sparse: cast to restricted __le32
>> fs/exfat/super.c:485:29: sparse: sparse: cast to restricted __le32
>> fs/exfat/super.c:485:29: sparse: sparse: cast to restricted __le32
>> fs/exfat/super.c:485:29: sparse: sparse: cast to restricted __le32
>> fs/exfat/super.c:485:29: sparse: sparse: cast to restricted __le32
>> fs/exfat/super.c:485:29: sparse: sparse: cast to restricted __le32

fs/exfat/super.c:504:21: sparse: sparse: cast to restricted __le32 fs/exfat/super.c:504:21: sparse: sparse: cast to restricted __le32 fs/exfat/super.c:504:21: sparse: sparse: cast to restricted __le32 fs/exfat/super.c:504:21: sparse: sparse: cast to restricted __le32 fs/exfat/super.c:504:21: sparse: sparse: cast to restricted __le32 fs/exfat/super.c:504:21: sparse: sparse: cast to restricted __le32

vim +485 fs/exfat/super.c

469
470 static int exfat_verify_boot_region(struct super_block *sb)
471 {
472 struct buffer_head *bh = NULL;
473 u32 chksum = 0, *p_sig, *p_chksum;
474 int sn, i;
475
476 /* read boot sector sub-regions */
477 for (sn = 0; sn < 11; sn++) {
478 bh = sb_bread(sb, sn);
479 if (!bh)
480 return -EIO;
481
482 if (sn != 0 && sn <= 8) {
483 /* extended boot sector sub-regions */
484 p_sig = (u32 *)&bh->b_data[sb->s_blocksize - 4];

 > 485				if (le32_to_cpu(*p_sig) != EXBOOT_SIGNATURE) {

486 exfat_err(sb, "no exboot-signature");
487 brelse(bh);
488 return -EINVAL;
489 }
490 }
491
492 chksum = exfat_calc_chksum32(bh->b_data, sb->s_blocksize,
493 chksum, sn ? CS_DEFAULT : CS_BOOT_SECTOR);
494 brelse(bh);
495 }
496
497 /* boot checksum sub-regions */
498 bh = sb_bread(sb, sn);
499 if (!bh)
500 return -EIO;
501
502 for (i = 0; i < sb->s_blocksize; i += sizeof(u32)) {
503 p_chksum = (u32 *)&bh->b_data[i];
504 if (le32_to_cpu(*p_chksum) != chksum) {
505 exfat_err(sb, "mismatch checksum");
506 brelse(bh);
507 return -EINVAL;
508 }
509 }
510 brelse(bh);
511 return 0;
512 }
513

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#5 lore raw

PATCH 4/4 exfat: standardize checksum calculation

In reply to Tetsuhiro Kohada

To clarify that it is a 16-bit checksum, the parts related to the 16-bit checksum are renamed and change type to u16.
Furthermore, replace checksum calculation in exfat_load_upcase_table() with exfat_calc_checksum32().

---
 fs/exfat/dir.c      | 12 ++++++------
 fs/exfat/exfat_fs.h |  5 ++---
 fs/exfat/misc.c     | 10 ++++------
 fs/exfat/nls.c      | 19 +++++++------------
 4 files changed, 19 insertions(+), 27 deletions(-)
fs/exfat/dir.c +6 -6
index b5a237c33d50..b673362a895c 100644
@@ -496,7 +496,7 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
 	int ret = 0;
 	int i, num_entries;
 	sector_t sector;
-	unsigned short chksum;
+	u16 chksum;
 	struct exfat_dentry *ep, *fep;
 	struct buffer_head *fbh, *bh;
 
@@ -505,7 +505,7 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
 		return -EIO;
 
 	num_entries = fep->dentry.file.num_ext + 1;
-	chksum = exfat_calc_chksum_2byte(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
+	chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
 
 	for (i = 1; i < num_entries; i++) {
 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL);
@@ -513,7 +513,7 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
 			ret = -EIO;
 			goto release_fbh;
 		}
-		chksum = exfat_calc_chksum_2byte(ep, DENTRY_SIZE, chksum,
+		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
 				CS_DEFAULT);
 		brelse(bh);
 	}
@@ -600,10 +600,10 @@ int exfat_update_dir_chksum_with_entry_set(struct super_block *sb,
 	int chksum_type = CS_DIR_ENTRY, i, num_entries = es->num_entries;
 	unsigned int buf_off = (off - es->offset);
 	unsigned int remaining_byte_in_sector, copy_entries, clu;
-	unsigned short chksum = 0;
+	u16 chksum = 0;
 
 	for (i = 0; i < num_entries; i++) {
-		chksum = exfat_calc_chksum_2byte(&es->entries[i], DENTRY_SIZE,
+		chksum = exfat_calc_chksum16(&es->entries[i], DENTRY_SIZE,
 			chksum, chksum_type);
 		chksum_type = CS_DEFAULT;
 	}
@@ -1047,7 +1047,7 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
 			}
 
 			if (entry_type == TYPE_STREAM) {
-				unsigned short name_hash;
+				u16 name_hash;
 
 				if (step != DIRENT_STEP_STRM) {
 					step = DIRENT_STEP_FILE;
fs/exfat/exfat_fs.h +2 -3
index 15817281b3c8..993d13bbebec 100644
@@ -139,7 +139,7 @@ struct exfat_dentry_namebuf {
 struct exfat_uni_name {
 	/* +3 for null and for converting */
 	unsigned short name[MAX_NAME_LENGTH + 3];
-	unsigned short name_hash;
+	u16 name_hash;
 	unsigned char name_len;
 };
 
@@ -515,8 +515,7 @@ void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
 void exfat_truncate_atime(struct timespec64 *ts);
 void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
 		u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);
-unsigned short exfat_calc_chksum_2byte(void *data, int len,
-		unsigned short chksum, int type);
+u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type);
 u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);
 void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync);
 void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,
fs/exfat/misc.c +4 -6
index b82d2dd5bd7c..17d41f3d3709 100644
@@ -136,17 +136,15 @@ void exfat_truncate_atime(struct timespec64 *ts)
 	ts->tv_nsec = 0;
 }
 
-unsigned short exfat_calc_chksum_2byte(void *data, int len,
-		unsigned short chksum, int type)
+u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type)
 {
 	int i;
-	unsigned char *c = (unsigned char *)data;
+	u8 *c = (u8 *)data;
 
 	for (i = 0; i < len; i++, c++) {
-		if (((i == 2) || (i == 3)) && (type == CS_DIR_ENTRY))
+		if (unlikely(type == CS_DIR_ENTRY && (i == 2 || i == 3)))
 			continue;
-		chksum = (((chksum & 1) << 15) | ((chksum & 0xFFFE) >> 1)) +
-			(unsigned short)*c;
+		chksum = ((chksum << 15) | (chksum >> 1)) + *c;
 	}
 	return chksum;
 }
fs/exfat/nls.c +7 -12
index 1ebda90cbdd7..19321773dd07 100644
@@ -527,7 +527,7 @@ static int exfat_utf8_to_utf16(struct super_block *sb,
 
 	*uniname = '\0';
 	p_uniname->name_len = unilen;
-	p_uniname->name_hash = exfat_calc_chksum_2byte(upname, unilen << 1, 0,
+	p_uniname->name_hash = exfat_calc_chksum16(upname, unilen << 1, 0,
 			CS_DEFAULT);
 
 	if (p_lossy)
@@ -623,7 +623,7 @@ static int exfat_nls_to_ucs2(struct super_block *sb,
 
 	*uniname = '\0';
 	p_uniname->name_len = unilen;
-	p_uniname->name_hash = exfat_calc_chksum_2byte(upname, unilen << 1, 0,
+	p_uniname->name_hash = exfat_calc_chksum16(upname, unilen << 1, 0,
 			CS_DEFAULT);
 
 	if (p_lossy)
@@ -655,7 +655,8 @@ static int exfat_load_upcase_table(struct super_block *sb,
 {
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
 	unsigned int sect_size = sb->s_blocksize;
-	unsigned int i, index = 0, checksum = 0;
+	unsigned int i, index = 0;
+	u32 chksum = 0;
 	int ret;
 	unsigned char skip = false;
 	unsigned short *upcase_table;
@@ -681,13 +682,6 @@ static int exfat_load_upcase_table(struct super_block *sb,
 		for (i = 0; i < sect_size && index <= 0xFFFF; i += 2) {
 			unsigned short uni = get_unaligned_le16(bh->b_data + i);
 
-			checksum = ((checksum & 1) ? 0x80000000 : 0) +
-				(checksum >> 1) +
-				*(((unsigned char *)bh->b_data) + i);
-			checksum = ((checksum & 1) ? 0x80000000 : 0) +
-				(checksum >> 1) +
-				*(((unsigned char *)bh->b_data) + (i + 1));
-
 			if (skip) {
 				index += uni;
 				skip = false;
@@ -701,13 +695,14 @@ static int exfat_load_upcase_table(struct super_block *sb,
 			}
 		}
 		brelse(bh);
+		chksum = exfat_calc_chksum32(bh->b_data, i, chksum, CS_DEFAULT);
 	}
 
-	if (index >= 0xFFFF && utbl_checksum == checksum)
+	if (index >= 0xFFFF && utbl_checksum == chksum)
 		return 0;
 
 	exfat_err(sb, "failed to load upcase table (idx : 0x%08x, chksum : 0x%08x, utbl_chksum : 0x%08x)",
-		  index, checksum, utbl_checksum);
+		  index, chksum, utbl_checksum);
 	ret = -EINVAL;
 free_table:
 	exfat_free_upcase_table(sbi);
Signature · 1 line
2.25.1
#6 lore raw

RE: [PATCH 4/4] exfat: standardize checksum calculation

Quoted 5 lines

To clarify that it is a 16-bit checksum, the parts related to the 16-bit checksum are renamed and change type to u16.
Furthermore, replace checksum calculation in exfat_load_upcase_table() with exfat_calc_checksum32().

I can not apply this patch to exfat dev tree. Could you please check it ? patching file fs/exfat/dir.c
Hunk #1 succeeded at 491 (offset -5 lines).
Hunk #2 succeeded at 500 (offset -5 lines).
Hunk #3 succeeded at 508 (offset -5 lines).
Hunk #4 FAILED at 600.
Hunk #5 succeeded at 1000 (offset -47 lines).
1 out of 5 hunks FAILED -- saving rejects to file fs/exfat/dir.c.rej patching file fs/exfat/exfat_fs.h
Hunk #1 succeeded at 137 (offset -2 lines).
Hunk #2 succeeded at 512 (offset -3 lines).
patching file fs/exfat/misc.c
patching file fs/exfat/nls.c

Thanks!

Quoted 169 lines
---
 fs/exfat/dir.c      | 12 ++++++------
 fs/exfat/exfat_fs.h |  5 ++---
 fs/exfat/misc.c     | 10 ++++------
 fs/exfat/nls.c      | 19 +++++++------------
 4 files changed, 19 insertions(+), 27 deletions(-)
fs/exfat/dir.c +2 -2
@@ -496,7 +496,7 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
 	int ret = 0;
 	int i, num_entries;
 	sector_t sector;
-	unsigned short chksum;
+	u16 chksum;
 	struct exfat_dentry *ep, *fep;
 	struct buffer_head *fbh, *bh;
 
@@ -505,7 +505,7 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
 		return -EIO;
 
 	num_entries = fep->dentry.file.num_ext + 1;
-	chksum = exfat_calc_chksum_2byte(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
+	chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
 
 	for (i = 1; i < num_entries; i++) {
 		ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL); @@ -513,7 +513,7 @@ int

exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,

fs/exfat/dir.c +4 -4
 			ret = -EIO;
 			goto release_fbh;
 		}
-		chksum = exfat_calc_chksum_2byte(ep, DENTRY_SIZE, chksum,
+		chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
 				CS_DEFAULT);
 		brelse(bh);
 	}
@@ -600,10 +600,10 @@ int exfat_update_dir_chksum_with_entry_set(struct super_block *sb,
 	int chksum_type = CS_DIR_ENTRY, i, num_entries = es->num_entries;
 	unsigned int buf_off = (off - es->offset);
 	unsigned int remaining_byte_in_sector, copy_entries, clu;
-	unsigned short chksum = 0;
+	u16 chksum = 0;
 
 	for (i = 0; i < num_entries; i++) {
-		chksum = exfat_calc_chksum_2byte(&es->entries[i], DENTRY_SIZE,
+		chksum = exfat_calc_chksum16(&es->entries[i], DENTRY_SIZE,
 			chksum, chksum_type);
 		chksum_type = CS_DEFAULT;
 	}
@@ -1047,7 +1047,7 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
 			}
 
 			if (entry_type == TYPE_STREAM) {
-				unsigned short name_hash;
+				u16 name_hash;
 
 				if (step != DIRENT_STEP_STRM) {
 					step = DIRENT_STEP_FILE;
fs/exfat/exfat_fs.h +2 -2
@@ -139,7 +139,7 @@ struct exfat_dentry_namebuf {  struct exfat_uni_name {
 	/* +3 for null and for converting */
 	unsigned short name[MAX_NAME_LENGTH + 3];
-	unsigned short name_hash;
+	u16 name_hash;
 	unsigned char name_len;
 };
 
@@ -515,8 +515,7 @@ void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,  void
 exfat_truncate_atime(struct timespec64 *ts);  void exfat_set_entry_time(struct exfat_sb_info *sbi,
 struct timespec64 *ts,
 		u8 *tz, __le16 *time, __le16 *date, u8 *time_cs); -unsigned short
 exfat_calc_chksum_2byte(void *data, int len,
-		unsigned short chksum, int type);
+u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type);
 u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);  void exfat_update_bh(struct
 super_block *sb, struct buffer_head *bh, int sync);  void exfat_chain_set(struct exfat_chain *ec,

unsigned int dir, diff --git a/fs/exfat/misc.c b/fs/exfat/misc.c index b82d2dd5bd7c..17d41f3d3709 100644

fs/exfat/misc.c +4 -6
@@ -136,17 +136,15 @@ void exfat_truncate_atime(struct timespec64 *ts)
 	ts->tv_nsec = 0;
 }
 
-unsigned short exfat_calc_chksum_2byte(void *data, int len,
-		unsigned short chksum, int type)
+u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type)
 {
 	int i;
-	unsigned char *c = (unsigned char *)data;
+	u8 *c = (u8 *)data;
 
 	for (i = 0; i < len; i++, c++) {
-		if (((i == 2) || (i == 3)) && (type == CS_DIR_ENTRY))
+		if (unlikely(type == CS_DIR_ENTRY && (i == 2 || i == 3)))
 			continue;
-		chksum = (((chksum & 1) << 15) | ((chksum & 0xFFFE) >> 1)) +
-			(unsigned short)*c;
+		chksum = ((chksum << 15) | (chksum >> 1)) + *c;
 	}
 	return chksum;
 }
fs/exfat/nls.c +7 -12
@@ -527,7 +527,7 @@ static int exfat_utf8_to_utf16(struct super_block *sb,
 
 	*uniname = '\0';
 	p_uniname->name_len = unilen;
-	p_uniname->name_hash = exfat_calc_chksum_2byte(upname, unilen << 1, 0,
+	p_uniname->name_hash = exfat_calc_chksum16(upname, unilen << 1, 0,
 			CS_DEFAULT);
 
 	if (p_lossy)
@@ -623,7 +623,7 @@ static int exfat_nls_to_ucs2(struct super_block *sb,
 
 	*uniname = '\0';
 	p_uniname->name_len = unilen;
-	p_uniname->name_hash = exfat_calc_chksum_2byte(upname, unilen << 1, 0,
+	p_uniname->name_hash = exfat_calc_chksum16(upname, unilen << 1, 0,
 			CS_DEFAULT);
 
 	if (p_lossy)
@@ -655,7 +655,8 @@ static int exfat_load_upcase_table(struct super_block *sb,  {
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
 	unsigned int sect_size = sb->s_blocksize;
-	unsigned int i, index = 0, checksum = 0;
+	unsigned int i, index = 0;
+	u32 chksum = 0;
 	int ret;
 	unsigned char skip = false;
 	unsigned short *upcase_table;
@@ -681,13 +682,6 @@ static int exfat_load_upcase_table(struct super_block *sb,
 		for (i = 0; i < sect_size && index <= 0xFFFF; i += 2) {
 			unsigned short uni = get_unaligned_le16(bh->b_data + i);
 
-			checksum = ((checksum & 1) ? 0x80000000 : 0) +
-				(checksum >> 1) +
-				*(((unsigned char *)bh->b_data) + i);
-			checksum = ((checksum & 1) ? 0x80000000 : 0) +
-				(checksum >> 1) +
-				*(((unsigned char *)bh->b_data) + (i + 1));
-
 			if (skip) {
 				index += uni;
 				skip = false;
@@ -701,13 +695,14 @@ static int exfat_load_upcase_table(struct super_block *sb,
 			}
 		}
 		brelse(bh);
+		chksum = exfat_calc_chksum32(bh->b_data, i, chksum, CS_DEFAULT);
 	}
 
-	if (index >= 0xFFFF && utbl_checksum == checksum)
+	if (index >= 0xFFFF && utbl_checksum == chksum)
 		return 0;
 
 	exfat_err(sb, "failed to load upcase table (idx : 0x%08x, chksum : 0x%08x, utbl_chksum :
 0x%08x)",
-		  index, checksum, utbl_checksum);
+		  index, chksum, utbl_checksum);
 	ret = -EINVAL;
 free_table:

exfat_free_upcase_table(sbi);
--
2.25.1

#7 lore raw

Re: [PATCH 4/4] exfat: standardize checksum calculation

Thank you for your comment.

Quoted 5 lines

I can not apply this patch to exfat dev tree. Could you please check it ? patching file fs/exfat/dir.c
Hunk #1 succeeded at 491 (offset -5 lines).
Hunk #2 succeeded at 500 (offset -5 lines).
Hunk #3 succeeded at 508 (offset -5 lines).

Quoted 8 lines

Hunk #4 FAILED at 600.
Hunk #5 succeeded at 1000 (offset -47 lines).
1 out of 5 hunks FAILED -- saving rejects to file fs/exfat/dir.c.rej patching file fs/exfat/exfat_fs.h
Hunk #1 succeeded at 137 (offset -2 lines).
Hunk #2 succeeded at 512 (offset -3 lines).
patching file fs/exfat/misc.c
patching file fs/exfat/nls.c

II tried applying patch to dev-tree (4c4dbb6ad8e8). -The .patch file I sent
-mbox file downloaded from archive
But I can't reproduce the error. (Both succeed) How do you reproduce the error?

BR

maintainer
#8 lore raw

Re: [PATCH 4/4] exfat: standardize checksum calculation

2020-05-27 16:39 GMT+09:00, Tetsuhiro Kohada <[email protected]>:

Quoted 19 lines

Thank you for your comment.

Quoted 5 lines

I can not apply this patch to exfat dev tree. Could you please check it ? patching file fs/exfat/dir.c
Hunk #1 succeeded at 491 (offset -5 lines).
Hunk #2 succeeded at 500 (offset -5 lines).
Hunk #3 succeeded at 508 (offset -5 lines).

Quoted 8 lines

Hunk #4 FAILED at 600.
Hunk #5 succeeded at 1000 (offset -47 lines).
1 out of 5 hunks FAILED -- saving rejects to file fs/exfat/dir.c.rej patching file fs/exfat/exfat_fs.h
Hunk #1 succeeded at 137 (offset -2 lines).
Hunk #2 succeeded at 512 (offset -3 lines).
patching file fs/exfat/misc.c
patching file fs/exfat/nls.c

II tried applying patch to dev-tree (4c4dbb6ad8e8).

(quoted diff) +0 -2
-The .patch file I sent
-mbox file downloaded from archive
Quoted 2 lines

But I can't reproduce the error. (Both succeed)
How do you reproduce the error?

I tried to appy your patches in the following order.
1. [PATCH] exfat: optimize dir-cache
2. [PATCH 1/4] exfat: redefine PBR as boot_sector
3. [PATCH 2/4] exfat: separate the boot sector analysis
4. [PATCH 3/4] exfat: add boot region verification
5. [PATCH 4/4] exfat: standardize checksum calculation

Thanks!

Quoted 3 lines

BR

#9 lore raw

Re: [PATCH 4/4] exfat: standardize checksum calculation

Quoted 11 lines
Quoted 5 lines

II tried applying patch to dev-tree (4c4dbb6ad8e8).

(quoted diff) +0 -2
-The .patch file I sent
-mbox file downloaded from archive

But I can't reproduce the error. (Both succeed)
How do you reproduce the error?

I tried to appy your patches in the following order.
1. [PATCH] exfat: optimize dir-cache
2. [PATCH 1/4] exfat: redefine PBR as boot_sector
3. [PATCH 2/4] exfat: separate the boot sector analysis
4. [PATCH 3/4] exfat: add boot region verification
5. [PATCH 4/4] exfat: standardize checksum calculation

I was able to reproduce it.

The dir-cache patch was created based on the HEAD of dev-tree. The 4 patches for boot_sector were also created based on the HEAD of dev-tree. (at physically separated place)

I'm sorry I didn't check any conflicts with these patches.

I'll repost the patch, based on the dir-cache patched dev-tree. If dir-cache patch will merge into dev-tree, should I wait until then?

BR

#10 lore raw

RE: [PATCH 4/4] exfat: standardize checksum calculation

Quoted 14 lines
Quoted 11 lines
Quoted 5 lines

II tried applying patch to dev-tree (4c4dbb6ad8e8).

(quoted diff) +0 -2
-The .patch file I sent
-mbox file downloaded from archive

But I can't reproduce the error. (Both succeed) How do you reproduce the error?

I tried to appy your patches in the following order.
1. [PATCH] exfat: optimize dir-cache
2. [PATCH 1/4] exfat: redefine PBR as boot_sector 3. [PATCH 2/4]
exfat: separate the boot sector analysis 4. [PATCH 3/4] exfat: add boot region verification 5. [PATCH 4/4] exfat: standardize checksum calculation

I was able to reproduce it.

Quoted 8 lines

The dir-cache patch was created based on the HEAD of dev-tree. The 4 patches for boot_sector were also created based on the HEAD of dev-tree. (at physically separated place)

I'm sorry I didn't check any conflicts with these patches.

I'll repost the patch, based on the dir-cache patched dev-tree. If dir-cache patch will merge into dev-tree, should I wait until then?

I will apply them after testing at once if you send updated 5 patches again. Thanks!

Quoted 2 lines

BR

#11 lore raw

Re: [PATCH 4/4] exfat: standardize checksum calculation

Quoted 3 lines
Quoted 2 lines

I'll repost the patch, based on the dir-cache patched dev-tree. If dir-cache patch will merge into dev-tree, should I wait until then?

I will apply them after testing at once if you send updated 5 patches again.

I resend patches for boot_sector.
However, the dir-cache patch hasn't changed, so I haven't reposted it.

BR

maintainer
#12 lore raw

Re: [PATCH 4/4] exfat: standardize checksum calculation

2020-05-28 19:09 GMT+09:00, Tetsuhiro Kohada <[email protected]>:

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

I'll repost the patch, based on the dir-cache patched dev-tree. If dir-cache patch will merge into dev-tree, should I wait until then?

I will apply them after testing at once if you send updated 5 patches again.

I resend patches for boot_sector.
However, the dir-cache patch hasn't changed, so I haven't reposted it.

Well, I leave a comment for this patch.

Quoted 3 lines

BR

maintainer
#13 lore raw
In reply to Tetsuhiro Kohada
Quoted 11 lines

Aggregate PBR related definitions and redefine as "boot_sector" to comply with the exFAT specification.
And, rename variable names including 'pbr'.

---
 fs/exfat/exfat_fs.h  |  2 +-
 fs/exfat/exfat_raw.h | 79 +++++++++++++++--------------------------
 fs/exfat/super.c     | 84 ++++++++++++++++++++++----------------------
 3 files changed, 72 insertions(+), 93 deletions(-)

[snip]

fs/exfat/exfat_raw.h line 82

Quoted 4 lines
fs/exfat/exfat_raw.h +4 -0
+/* EXFAT: Main and Backup Boot Sector (512 bytes) */ struct boot_sector
+{
+	__u8	jmp_boot[BOOTSEC_JUMP_BOOT_LEN];
+	__u8	oem_name[BOOTSEC_OEM_NAME_LEN];

According to the exFAT specification, fs_name and BOOTSEC_FS_NAME_LEN look better.

Quoted 20 lines
fs/exfat/exfat_raw.h +19 -0
+	__u8	must_be_zero[BOOTSEC_OLDBPB_LEN];
+	__le64	partition_offset;
+	__le64	vol_length;
+	__le32	fat_offset;
+	__le32	fat_length;
+	__le32	clu_offset;
+	__le32	clu_count;
+	__le32	root_cluster;
+	__le32	vol_serial;
+	__u8	fs_revision[2];
+	__le16	vol_flags;
+	__u8	sect_size_bits;
+	__u8	sect_per_clus_bits;
+	__u8	num_fats;
+	__u8	drv_sel;
+	__u8	percent_in_use;
+	__u8	reserved[7];
+	__u8	boot_code[390];
+	__le16	signature;
 } __packed;
#14 lore raw
Quoted 8 lines

[snip]

fs/exfat/exfat_raw.h line 82

Quoted 4 lines
fs/exfat/exfat_raw.h +4 -0
+/* EXFAT: Main and Backup Boot Sector (512 bytes) */ struct boot_sector
+{
+	__u8	jmp_boot[BOOTSEC_JUMP_BOOT_LEN];
+	__u8	oem_name[BOOTSEC_OEM_NAME_LEN];

According to the exFAT specification, fs_name and BOOTSEC_FS_NAME_LEN look better.

Oops.
I sent v2 patches, before I noticed this comment,

I'll make another small patch, OK?

BTW
I have a concern about fs_name.
The exfat specification says that this field is "EXFAT".

I think it's a important field for determining the filesystem. However, in this patch, I gave up checking this field. Because there is no similar check in FATFS.
Do you know why Linux FATFS does not check this filed? And, what do you think of checking this field?

BR

maintainer
#15 lore raw
Quoted 9 lines
Quoted 8 lines

[snip]

fs/exfat/exfat_raw.h line 82

Quoted 4 lines
fs/exfat/exfat_raw.h +4 -0
+/* EXFAT: Main and Backup Boot Sector (512 bytes) */ struct
+boot_sector {
+	__u8	jmp_boot[BOOTSEC_JUMP_BOOT_LEN];
+	__u8	oem_name[BOOTSEC_OEM_NAME_LEN];

According to the exFAT specification, fs_name and BOOTSEC_FS_NAME_LEN look better.

Quoted 4 lines

Oops.
I sent v2 patches, before I noticed this comment,

I'll make another small patch, OK?

No, It make sense to make v3, because you have renamed the variables in boot_sector on this patch.

Quoted 9 lines

BTW
I have a concern about fs_name.
The exfat specification says that this field is "EXFAT".

I think it's a important field for determining the filesystem. However, in this patch, I gave up checking this field. Because there is no similar check in FATFS.
Do you know why Linux FATFS does not check this filed? And, what do you think of checking this field?

FATFS has the same field named "oem_name" and whatever is okay for its value. However, in case of exFAT, it is an important field to determine filesystem.

I think it would be better to check this field for exFAT-fs. Would you like to contribute new patch for checking it?

Quoted 2 lines

BR

#16 lore raw
Quoted 4 lines
Quoted 1 line

I'll make another small patch, OK?

No, It make sense to make v3, because you have renamed the variables in boot_sector on this patch.

OK.

Quoted 10 lines
Quoted 9 lines

BTW
I have a concern about fs_name.
The exfat specification says that this field is "EXFAT".

I think it's a important field for determining the filesystem. However, in this patch, I gave up checking this field. Because there is no similar check in FATFS.
Do you know why Linux FATFS does not check this filed? And, what do you think of checking this field?

Quoted 5 lines

FATFS has the same field named "oem_name" and whatever is okay for its value. However, in case of exFAT, it is an important field to determine filesystem.

I think it would be better to check this field for exFAT-fs. Would you like to contribute new patch for checking it?

I already have the code, so I'll add it to [PATCH 2/4 v3].

BR