[RFC PATCH 7/7] exfat: get mutil-clusters in exfat_get_block

Chi Zhiling posted 7 patches 1 week, 6 days ago
[RFC PATCH 7/7] exfat: get mutil-clusters in exfat_get_block
Posted by Chi Zhiling 1 week, 6 days ago
From: Chi Zhiling <chizhiling@kylinos.cn>

mpage uses the get_block of the file system to obtain the mapping of a
file or allocate blocks for writes. Currently exfat only supports
obtaining one cluster in each get_block call.

Since exfat_count_contig_clusters can obtain multiple consecutive clusters,
it can be used to improve exfat_get_block when page size is larger than
cluster size.

Signed-off-by: Chi Zhiling <chizhiling@kylinos.cn>
---
 fs/exfat/inode.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c
index f9501c3a3666..256ba2af34eb 100644
--- a/fs/exfat/inode.c
+++ b/fs/exfat/inode.c
@@ -264,13 +264,14 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
 static int exfat_get_block(struct inode *inode, sector_t iblock,
 		struct buffer_head *bh_result, int create)
 {
+	struct exfat_chain chain;
 	struct exfat_inode_info *ei = EXFAT_I(inode);
 	struct super_block *sb = inode->i_sb;
 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
 	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
 	int err = 0;
 	unsigned long mapped_blocks = 0;
-	unsigned int cluster, sec_offset;
+	unsigned int cluster, sec_offset, count;
 	sector_t last_block;
 	sector_t phys = 0;
 	sector_t valid_blks;
@@ -301,6 +302,17 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
 
 	phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
 	mapped_blocks = sbi->sect_per_clus - sec_offset;
+
+	if (max_blocks > mapped_blocks && !create) {
+		chain.dir = cluster;
+		chain.size = (max_blocks >> sbi->sect_per_clus_bits) + 1;
+		chain.flags = ei->flags;
+
+		err = exfat_count_contig_clusters(sb, &chain, &count);
+		if (err)
+			return err;
+		max_blocks = (count << sbi->sect_per_clus_bits) - sec_offset;
+	}
 	max_blocks = min(mapped_blocks, max_blocks);
 
 	map_bh(bh_result, sb, phys);
-- 
2.43.0
Re: [RFC PATCH 7/7] exfat: get mutil-clusters in exfat_get_block
Posted by Sungjong Seo 3 days, 23 hours ago
Hi, Chi,
On 25. 11. 18. 17:22, Chi Zhiling wrote:
> From: Chi Zhiling <chizhiling@kylinos.cn>
> 
> mpage uses the get_block of the file system to obtain the mapping of a
> file or allocate blocks for writes. Currently exfat only supports
> obtaining one cluster in each get_block call.
> 
> Since exfat_count_contig_clusters can obtain multiple consecutive clusters,
> it can be used to improve exfat_get_block when page size is larger than
> cluster size.

I think reusing buffer_head is a good approach!
However, for obtaining multiple clusters, it would be better to handle
them in exfat_map_cluster.

> 
> Signed-off-by: Chi Zhiling <chizhiling@kylinos.cn>
> ---
>  fs/exfat/inode.c | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c
> index f9501c3a3666..256ba2af34eb 100644
> --- a/fs/exfat/inode.c
> +++ b/fs/exfat/inode.c
> @@ -264,13 +264,14 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
>  static int exfat_get_block(struct inode *inode, sector_t iblock,
>  		struct buffer_head *bh_result, int create)
>  {
> +	struct exfat_chain chain;
>  	struct exfat_inode_info *ei = EXFAT_I(inode);
>  	struct super_block *sb = inode->i_sb;
>  	struct exfat_sb_info *sbi = EXFAT_SB(sb);
>  	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
>  	int err = 0;
>  	unsigned long mapped_blocks = 0;
> -	unsigned int cluster, sec_offset;
> +	unsigned int cluster, sec_offset, count;
>  	sector_t last_block;
>  	sector_t phys = 0;
>  	sector_t valid_blks;
> @@ -301,6 +302,17 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
>  
>  	phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
>  	mapped_blocks = sbi->sect_per_clus - sec_offset;
> +
> +	if (max_blocks > mapped_blocks && !create) {
> +		chain.dir = cluster;
> +		chain.size = (max_blocks >> sbi->sect_per_clus_bits) + 1;

There seems to be an issue where the code sets chain.size to be one greater than the actual cluster count.

For example, assuming a 16KiB page, 512B sector, and 4KiB cluster,
for a 16KiB file, chain.size becomes 5 instead of 4.
Is this the intended behavior?

> +		chain.flags = ei->flags;
> +
> +		err = exfat_count_contig_clusters(sb, &chain, &count);
> +		if (err)
> +			return err;
> +		max_blocks = (count << sbi->sect_per_clus_bits) - sec_offset;

You already said mapped_blocks is correct.

> +	}
>  	max_blocks = min(mapped_blocks, max_blocks);
>  
>  	map_bh(bh_result, sb, phys);
Re: [RFC PATCH 7/7] exfat: get mutil-clusters in exfat_get_block
Posted by Chi Zhiling 3 days, 20 hours ago
On 11/28/25 10:48, Sungjong Seo wrote:
> 
> Hi, Chi,
> On 25. 11. 18. 17:22, Chi Zhiling wrote:
>> From: Chi Zhiling <chizhiling@kylinos.cn>
>>
>> mpage uses the get_block of the file system to obtain the mapping of a
>> file or allocate blocks for writes. Currently exfat only supports
>> obtaining one cluster in each get_block call.
>>
>> Since exfat_count_contig_clusters can obtain multiple consecutive clusters,
>> it can be used to improve exfat_get_block when page size is larger than
>> cluster size.
> 
> I think reusing buffer_head is a good approach!
> However, for obtaining multiple clusters, it would be better to handle
> them in exfat_map_cluster.

Hi, Sungjong

I agree.

My original plan was to support multiple clusters for exfat_map_cluster 
and exfat_get_cluster. since the changes required were quite extensive, 
I put that plan on hold. This would likely involve refactoring 
exfat_map_clusterand introducing iterators to reduce the number of 
parameters it needs

I will take some time to consider the signature of the new 
exfat_map_clusters. Do you have any thoughts about this?

> 
>>
>> Signed-off-by: Chi Zhiling <chizhiling@kylinos.cn>
>> ---
>>   fs/exfat/inode.c | 14 +++++++++++++-
>>   1 file changed, 13 insertions(+), 1 deletion(-)
>>
>> diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c
>> index f9501c3a3666..256ba2af34eb 100644
>> --- a/fs/exfat/inode.c
>> +++ b/fs/exfat/inode.c
>> @@ -264,13 +264,14 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
>>   static int exfat_get_block(struct inode *inode, sector_t iblock,
>>   		struct buffer_head *bh_result, int create)
>>   {
>> +	struct exfat_chain chain;
>>   	struct exfat_inode_info *ei = EXFAT_I(inode);
>>   	struct super_block *sb = inode->i_sb;
>>   	struct exfat_sb_info *sbi = EXFAT_SB(sb);
>>   	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
>>   	int err = 0;
>>   	unsigned long mapped_blocks = 0;
>> -	unsigned int cluster, sec_offset;
>> +	unsigned int cluster, sec_offset, count;
>>   	sector_t last_block;
>>   	sector_t phys = 0;
>>   	sector_t valid_blks;
>> @@ -301,6 +302,17 @@ static int exfat_get_block(struct inode *inode, sector_t iblock,
>>   
>>   	phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
>>   	mapped_blocks = sbi->sect_per_clus - sec_offset;
>> +
>> +	if (max_blocks > mapped_blocks && !create) {
>> +		chain.dir = cluster;
>> +		chain.size = (max_blocks >> sbi->sect_per_clus_bits) + 1;
> 
> There seems to be an issue where the code sets chain.size to be one greater than the actual cluster count.
> 
> For example, assuming a 16KiB page, 512B sector, and 4KiB cluster,
> for a 16KiB file, chain.size becomes 5 instead of 4.
> Is this the intended behavior?

This is not the expected behavior. It's a serious bug. Thank you very 
much for pointing this out.

> 
>> +		chain.flags = ei->flags;
>> +
>> +		err = exfat_count_contig_clusters(sb, &chain, &count);
>> +		if (err)
>> +			return err;
>> +		max_blocks = (count << sbi->sect_per_clus_bits) - sec_offset;
> 
> You already said mapped_blocks is correct.
> 
>> +	}
>>   	max_blocks = min(mapped_blocks, max_blocks);
>>   
>>   	map_bh(bh_result, sb, phys);