exfat: use exfat_chain_advance helper

Replace open-coded cluster chain walking logic with exfat_chain_advance()
across exfat_readdir, exfat_find_dir_entry, exfat_count_dir_entries,
exfat_search_empty_slot and exfat_check_dir_empty.

Signed-off-by: Chi Zhiling <chizhiling@kylinos.cn>
Reviewed-by: Sungjong Seo <sj1557.seo@samsung.com>
Reviewed-by: Yuezhang Mo <Yuezhang.Mo@sony.com>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
This commit is contained in:
Chi Zhiling
2026-04-03 16:05:38 +08:00
committed by Namjae Jeon
parent 227468fc82
commit 08cf4a8181
2 changed files with 25 additions and 74 deletions

View File

@@ -93,25 +93,19 @@ static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_ent
clu_offset = EXFAT_DEN_TO_CLU(dentry, sbi);
exfat_chain_dup(&clu, &dir);
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
clu.dir += clu_offset;
clu.size -= clu_offset;
} else {
if (clu.flags == ALLOC_FAT_CHAIN) {
/* hint_information */
if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
clu_offset -= ei->hint_bmap.off;
clu.dir = ei->hint_bmap.clu;
}
while (clu_offset > 0 && clu.dir != EXFAT_EOF_CLUSTER) {
if (exfat_get_next_cluster(sb, &(clu.dir)))
return -EIO;
clu_offset--;
clu.size -= ei->hint_bmap.off;
}
}
if (exfat_chain_advance(sb, &clu, clu_offset))
return -EIO;
while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
i = dentry & (dentries_per_clu - 1);
@@ -160,15 +154,8 @@ static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_ent
return 0;
}
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
if (--clu.size > 0)
clu.dir++;
else
clu.dir = EXFAT_EOF_CLUSTER;
} else {
if (exfat_get_next_cluster(sb, &(clu.dir)))
return -EIO;
}
if (exfat_chain_advance(sb, &clu, 1))
return -EIO;
}
out:
@@ -1085,19 +1072,12 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
step = DIRENT_STEP_FILE;
}
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
if (--clu.size > 0)
clu.dir++;
else
clu.dir = EXFAT_EOF_CLUSTER;
} else {
if (exfat_get_next_cluster(sb, &clu.dir))
return -EIO;
if (exfat_chain_advance(sb, &clu, 1))
return -EIO;
/* break if the cluster chain includes a loop */
if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
goto not_found;
}
/* break if the cluster chain includes a loop */
if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
goto not_found;
}
not_found:
@@ -1132,14 +1112,7 @@ int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
if (!((dentry + 1) & (dentries_per_clu - 1))) {
int ret = 0;
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
if (--clu.size > 0)
clu.dir++;
else
clu.dir = EXFAT_EOF_CLUSTER;
} else {
ret = exfat_get_next_cluster(sb, &clu.dir);
}
ret = exfat_chain_advance(sb, &clu, 1);
if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
/* just initialized hint_stat */
@@ -1184,20 +1157,12 @@ int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
count++;
}
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
if (--clu.size > 0)
clu.dir++;
else
clu.dir = EXFAT_EOF_CLUSTER;
} else {
if (exfat_get_next_cluster(sb, &(clu.dir)))
return -EIO;
if (unlikely(++clu_count > sbi->used_clusters)) {
exfat_fs_error(sb, "FAT or bitmap is corrupted");
return -EIO;
}
if (exfat_chain_advance(sb, &clu, 1))
return -EIO;
if (unlikely(++clu_count > sbi->used_clusters)) {
exfat_fs_error(sb, "FAT or bitmap is corrupted");
return -EIO;
}
}

View File

@@ -246,15 +246,8 @@ static int exfat_search_empty_slot(struct super_block *sb,
i += ret;
while (i >= dentries_per_clu) {
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
if (--clu.size > 0)
clu.dir++;
else
clu.dir = EXFAT_EOF_CLUSTER;
} else {
if (exfat_get_next_cluster(sb, &clu.dir))
return -EIO;
}
if (exfat_chain_advance(sb, &clu, 1))
return -EIO;
i -= dentries_per_clu;
}
@@ -925,19 +918,12 @@ static int exfat_check_dir_empty(struct super_block *sb,
return -ENOTEMPTY;
}
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
if (--clu.size > 0)
clu.dir++;
else
clu.dir = EXFAT_EOF_CLUSTER;
} else {
if (exfat_get_next_cluster(sb, &(clu.dir)))
return -EIO;
if (exfat_chain_advance(sb, &clu, 1))
return -EIO;
/* break if the cluster chain includes a loop */
if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
break;
}
/* break if the cluster chain includes a loop */
if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
break;
}
return 0;