Skip to content

Commit 5875bf2

Browse files
Tetsuhiro Kohadanamjaejeon
authored andcommitted
exfat: standardize checksum calculation
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(). Signed-off-by: Tetsuhiro Kohada <[email protected]> Reviewed-by: Sungjong Seo <[email protected]> Signed-off-by: Namjae Jeon <[email protected]>
1 parent 476189c commit 5875bf2

File tree

4 files changed

+19
-27
lines changed

4 files changed

+19
-27
lines changed

fs/exfat/dir.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
491491
int ret = 0;
492492
int i, num_entries;
493493
sector_t sector;
494-
unsigned short chksum;
494+
u16 chksum;
495495
struct exfat_dentry *ep, *fep;
496496
struct buffer_head *fbh, *bh;
497497

@@ -500,15 +500,15 @@ int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
500500
return -EIO;
501501

502502
num_entries = fep->dentry.file.num_ext + 1;
503-
chksum = exfat_calc_chksum_2byte(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
503+
chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
504504

505505
for (i = 1; i < num_entries; i++) {
506506
ep = exfat_get_dentry(sb, p_dir, entry + i, &bh, NULL);
507507
if (!ep) {
508508
ret = -EIO;
509509
goto release_fbh;
510510
}
511-
chksum = exfat_calc_chksum_2byte(ep, DENTRY_SIZE, chksum,
511+
chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
512512
CS_DEFAULT);
513513
brelse(bh);
514514
}
@@ -593,8 +593,8 @@ void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
593593

594594
for (i = 0; i < es->num_entries; i++) {
595595
ep = exfat_get_dentry_cached(es, i);
596-
chksum = exfat_calc_chksum_2byte(ep, DENTRY_SIZE, chksum,
597-
chksum_type);
596+
chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
597+
chksum_type);
598598
chksum_type = CS_DEFAULT;
599599
}
600600
ep = exfat_get_dentry_cached(es, 0);
@@ -1000,7 +1000,7 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
10001000
}
10011001

10021002
if (entry_type == TYPE_STREAM) {
1003-
unsigned short name_hash;
1003+
u16 name_hash;
10041004

10051005
if (step != DIRENT_STEP_STRM) {
10061006
step = DIRENT_STEP_FILE;

fs/exfat/exfat_fs.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ struct exfat_dentry_namebuf {
137137
struct exfat_uni_name {
138138
/* +3 for null and for converting */
139139
unsigned short name[MAX_NAME_LENGTH + 3];
140-
unsigned short name_hash;
140+
u16 name_hash;
141141
unsigned char name_len;
142142
};
143143

@@ -512,8 +512,7 @@ void exfat_get_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
512512
void exfat_truncate_atime(struct timespec64 *ts);
513513
void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts,
514514
u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);
515-
unsigned short exfat_calc_chksum_2byte(void *data, int len,
516-
unsigned short chksum, int type);
515+
u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type);
517516
u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type);
518517
void exfat_update_bh(struct super_block *sb, struct buffer_head *bh, int sync);
519518
void exfat_chain_set(struct exfat_chain *ec, unsigned int dir,

fs/exfat/misc.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,15 @@ void exfat_truncate_atime(struct timespec64 *ts)
136136
ts->tv_nsec = 0;
137137
}
138138

139-
unsigned short exfat_calc_chksum_2byte(void *data, int len,
140-
unsigned short chksum, int type)
139+
u16 exfat_calc_chksum16(void *data, int len, u16 chksum, int type)
141140
{
142141
int i;
143-
unsigned char *c = (unsigned char *)data;
142+
u8 *c = (u8 *)data;
144143

145144
for (i = 0; i < len; i++, c++) {
146-
if (((i == 2) || (i == 3)) && (type == CS_DIR_ENTRY))
145+
if (unlikely(type == CS_DIR_ENTRY && (i == 2 || i == 3)))
147146
continue;
148-
chksum = (((chksum & 1) << 15) | ((chksum & 0xFFFE) >> 1)) +
149-
(unsigned short)*c;
147+
chksum = ((chksum << 15) | (chksum >> 1)) + *c;
150148
}
151149
return chksum;
152150
}

fs/exfat/nls.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ static int exfat_utf8_to_utf16(struct super_block *sb,
527527

528528
*uniname = '\0';
529529
p_uniname->name_len = unilen;
530-
p_uniname->name_hash = exfat_calc_chksum_2byte(upname, unilen << 1, 0,
530+
p_uniname->name_hash = exfat_calc_chksum16(upname, unilen << 1, 0,
531531
CS_DEFAULT);
532532

533533
if (p_lossy)
@@ -623,7 +623,7 @@ static int exfat_nls_to_ucs2(struct super_block *sb,
623623

624624
*uniname = '\0';
625625
p_uniname->name_len = unilen;
626-
p_uniname->name_hash = exfat_calc_chksum_2byte(upname, unilen << 1, 0,
626+
p_uniname->name_hash = exfat_calc_chksum16(upname, unilen << 1, 0,
627627
CS_DEFAULT);
628628

629629
if (p_lossy)
@@ -655,7 +655,8 @@ static int exfat_load_upcase_table(struct super_block *sb,
655655
{
656656
struct exfat_sb_info *sbi = EXFAT_SB(sb);
657657
unsigned int sect_size = sb->s_blocksize;
658-
unsigned int i, index = 0, checksum = 0;
658+
unsigned int i, index = 0;
659+
u32 chksum = 0;
659660
int ret;
660661
unsigned char skip = false;
661662
unsigned short *upcase_table;
@@ -681,13 +682,6 @@ static int exfat_load_upcase_table(struct super_block *sb,
681682
for (i = 0; i < sect_size && index <= 0xFFFF; i += 2) {
682683
unsigned short uni = get_unaligned_le16(bh->b_data + i);
683684

684-
checksum = ((checksum & 1) ? 0x80000000 : 0) +
685-
(checksum >> 1) +
686-
*(((unsigned char *)bh->b_data) + i);
687-
checksum = ((checksum & 1) ? 0x80000000 : 0) +
688-
(checksum >> 1) +
689-
*(((unsigned char *)bh->b_data) + (i + 1));
690-
691685
if (skip) {
692686
index += uni;
693687
skip = false;
@@ -701,13 +695,14 @@ static int exfat_load_upcase_table(struct super_block *sb,
701695
}
702696
}
703697
brelse(bh);
698+
chksum = exfat_calc_chksum32(bh->b_data, i, chksum, CS_DEFAULT);
704699
}
705700

706-
if (index >= 0xFFFF && utbl_checksum == checksum)
701+
if (index >= 0xFFFF && utbl_checksum == chksum)
707702
return 0;
708703

709704
exfat_err(sb, "failed to load upcase table (idx : 0x%08x, chksum : 0x%08x, utbl_chksum : 0x%08x)",
710-
index, checksum, utbl_checksum);
705+
index, chksum, utbl_checksum);
711706
ret = -EINVAL;
712707
free_table:
713708
exfat_free_upcase_table(sbi);

0 commit comments

Comments
 (0)