exfat: improve performance of NO_FAT_CHAIN to FAT_CHAIN conversion
Subsystems (2)
v2 cover exfat: improve performance of NO_FAT_CHAIN to FAT_CHAIN conversion
From: Chi Zhiling <[email protected]>
This series improves the performance when converting files from NO_FAT_CHAIN to FAT_CHAIN format (which happens when a file cannot allocate contiguous clusters). It also adds proper error handling for FAT table write operations.
- Patch 1: Add block readahead for FAT blocks during conversion
- Patch 2-3: Refactor existing readahead code to use the new helper
- Patch 4: Remove redundant sec parameter from exfat_mirror_bh
- Patch 5: Cache buffer heads to reduce mark_buffer_dirty overhead
- Patch 6: Fix error handling to propagate FAT write errors to callers
Performance improvements for converting a 30GB file:
| Cluster Size | Before | After | Speedup | |--------------|--------|--------|---------| | 512 bytes | 47.667s| 1.866s | 25.5x | | 4KB | 6.436s | 0.236s | 27.3x | | 32KB | 0.758s | 0.034s | 22.3x | | 256KB | 0.117s | 0.006s | 19.5x |
v1: https://lore.kernel.org/all/[email protected]/
Chi Zhiling (6):
exfat: add block readahead in exfat_chain_cont_cluster
exfat: use readahead helper in exfat_allocate_bitmap
exfat: use readahead helper in exfat_get_dentry
exfat: drop redundant sec parameter from exfat_mirror_bh
exfat: optimize exfat_chain_cont_cluster with cached buffer heads
exfat: fix error handling for FAT table operations
fs/exfat/balloc.c | 18 +++------ fs/exfat/dir.c | 52 +++++++------------------ fs/exfat/exfat_fs.h | 13 +++++-- fs/exfat/fatent.c | 95 ++++++++++++++++++++++++++++++++++++--------- fs/exfat/inode.c | 5 ++- fs/exfat/misc.c | 8 +++- fs/exfat/namei.c | 3 +- 7 files changed, 118 insertions(+), 76 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/6 exfat: add block readahead in exfat_chain_cont_cluster
From: Chi Zhiling <[email protected]>
When a file cannot allocate contiguous clusters, exfat converts the file from NO_FAT_CHAIN to FAT_CHAIN format. For large files, this conversion process can take a significant amount of time.
Add simple readahead to read all the FAT blocks in advance, as these blocks are consecutive, significantly improving the conversion performance.
Test in an empty exfat filesystem:
dd if=/dev/zero of=/mnt/file bs=1M count=30k
dd if=/dev/zero of=/mnt/file2 bs=1M count=1
time cat /mnt/file2 >> /mnt/file
| cluster size | before patch | after patch | | ------------ | ------------ | ----------- | | 512 | 47.667s | 4.316s | | 4k | 6.436s | 0.541s | | 32k | 0.758s | 0.071s | | 256k | 0.117s | 0.011s |
--- fs/exfat/exfat_fs.h | 11 +++++++++-- fs/exfat/fatent.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-)
fs/exfat/exfat_fs.h +9 -2
@@ -10,6 +10,7 @@#include <linux/ratelimit.h> #include <linux/nls.h> #include <linux/blkdev.h> +#include <linux/backing-dev.h> #include <uapi/linux/exfat.h> #define EXFAT_ROOT_INO 1
@@ -79,6 +80,10 @@ enum {#define EXFAT_HINT_NONE -1#define EXFAT_MIN_SUBDIR 2 +#define EXFAT_BLK_RA_SIZE (sb) \ + (min_t(blkcnt_t, (sb)->s_bdi->ra_pages, (sb)->s_bdi->io_pages) \ + << (PAGE_SHIFT - (sb)->s_blocksize_bits)) +/* * helpers for cluster size to byte conversion. */
@@ -117,9 +122,9 @@ enum {#define FAT_ENT_SIZE (4)#define FAT_ENT_SIZE_BITS (2)#define FAT_ENT_OFFSET_SECTOR (sb, loc) (EXFAT_SB(sb)->FAT1_start_sector + \ - (((u64)loc << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits)) + (((u64)(loc) << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits))#define FAT_ENT_OFFSET_BYTE_IN_SECTOR (sb, loc) \ - ((loc << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1)) + (((loc) << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1))/* * helpers for bitmap.
@@ -448,6 +453,8 @@ int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain, unsigned int *ret_clu);int exfat_count_num_clusters (struct super_block *sb,struct exfat_chain *p_chain,unsigned int *ret_count); +int exfat_blk_readahead (struct super_block *sb,sector_t sec, +sector_t *ra,blkcnt_t *ra_cnt,sector_t end);/* balloc.c */ int exfat_load_bitmap (struct super_block *sb);
fs/exfat/fatent.c +37 -0
@@ -142,13 +142,50 @@ int exfat_ent_get(struct super_block *sb, unsigned int loc,return -EIO ; } +int exfat_blk_readahead (struct super_block *sb,sector_t sec, +sector_t *ra,blkcnt_t *ra_cnt,sector_t end) +{ +struct blk_plug plug; + +if (sec < *ra) +return 0 ; + + *ra += *ra_cnt; + +/* No blocks left (or only the last block), skip readahead. */ +if (*ra >= end) +return 0 ; + + *ra_cnt =min (end - *ra +1 ,EXFAT_BLK_RA_SIZE (sb)); +if (*ra_cnt ==0 ) { +/* Move 'ra' to the end to disable readahead. */ + *ra = end; +return 0 ; + } + +blk_start_plug (&plug); +for (unsigned int i =0 ; i < *ra_cnt; i++) +sb_breadahead (sb, *ra + i); +blk_finish_plug (&plug); +return 0 ; +} +int exfat_chain_cont_cluster (struct super_block *sb,unsigned int chain,unsigned int len) { +sector_t sec, end, ra; +blkcnt_t ra_cnt =0 ; +if (!len)return 0 ; + ra =FAT_ENT_OFFSET_SECTOR (sb, chain); + end =FAT_ENT_OFFSET_SECTOR (sb, chain + len -1 ); +while (len >1 ) { + sec =FAT_ENT_OFFSET_SECTOR (sb, chain); +exfat_blk_readahead (sb, sec, &ra, &ra_cnt, end); +if (exfat_ent_set (sb, chain, chain +1 ))return -EIO ; chain++;
Signature · 1 line
2.43.0
PATCH v2 2/6 exfat: use readahead helper in exfat_allocate_bitmap
In reply to Chi ZhilingFrom: Chi Zhiling <[email protected]>
Use the newly added exfat_blk_readahead() helper in exfat_allocate_bitmap() to simplify the code. This eliminates the duplicate inline readahead logic and uses the unified readahead interface.
--- fs/exfat/balloc.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)
fs/exfat/balloc.c +6 -12
@@ -74,11 +74,10 @@ static int exfat_allocate_bitmap(struct super_block *sb, struct exfat_dentry *ep) {struct exfat_sb_info *sbi =EXFAT_SB (sb); -struct blk_plug plug;long long map_size;unsigned int i, j, need_map_size; -sector_t sector; -unsigned int max_ra_count; +sector_t sector, end, ra; +blkcnt_t ra_cnt =0 ; sbi->map_clu =le32_to_cpu (ep->dentry.bitmap.start_clu); map_size =le64_to_cpu (ep->dentry.bitmap.size);
@@ -100,17 +99,12 @@ static int exfat_allocate_bitmap(struct super_block *sb,if (!sbi->vol_amap)return -ENOMEM ; - sector =exfat_cluster_to_sector (sbi, sbi->map_clu); - max_ra_count =min (sb->s_bdi->ra_pages, sb->s_bdi->io_pages) << - (PAGE_SHIFT - sb->s_blocksize_bits); + sector = ra =exfat_cluster_to_sector (sbi, sbi->map_clu); + end = sector + sbi->map_sectors -1 ; +for (i =0 ; i < sbi->map_sectors; i++) {/* Trigger the next readahead in advance. */ -if (max_ra_count &&0 == (i % max_ra_count)) { -blk_start_plug (&plug); -for (j = i; j <min (max_ra_count, sbi->map_sectors - i) + i; j++) -sb_breadahead (sb, sector + j); -blk_finish_plug (&plug); - } +exfat_blk_readahead (sb, sector + i, &ra, &ra_cnt, end); sbi->vol_amap[i] =sb_bread (sb, sector + i);if (!sbi->vol_amap[i])
Signature · 1 line
2.43.0
PATCH v2 3/6 exfat: use readahead helper in exfat_get_dentry
In reply to Chi ZhilingFrom: Chi Zhiling <[email protected]>
Replace the custom exfat_dir_readahead() function with the unified exfat_blk_readahead() helper in exfat_get_dentry(). This removes the duplicate readahead implementation and uses the common interface, also reducing code complexity.
--- fs/exfat/dir.c | 52 ++++++++++++++------------------------------------ 1 file changed, 14 insertions(+), 38 deletions(-)
fs/exfat/dir.c +14 -38
@@ -623,44 +623,11 @@ static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dirreturn 0 ; } -#define EXFAT_MAX_RA_SIZE (128*1024) -static int exfat_dir_readahead (struct super_block *sb,sector_t sec) -{ -struct exfat_sb_info *sbi =EXFAT_SB (sb); -struct buffer_head *bh; -unsigned int max_ra_count =EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits; -unsigned int page_ra_count =PAGE_SIZE >> sb->s_blocksize_bits; -unsigned int adj_ra_count =max (sbi->sect_per_clus, page_ra_count); -unsigned int ra_count =min (adj_ra_count, max_ra_count); - -/* Read-ahead is not required */ -if (sbi->sect_per_clus ==1 ) -return 0 ; - -if (sec < sbi->data_start_sector) { -exfat_err (sb,"requested sector is invalid(sect:%llu, root:%llu)" , - (unsigned long long )sec, sbi->data_start_sector); -return -EIO ; - } - -/* Not sector aligned with ra_count, resize ra_count to page size */ -if ((sec - sbi->data_start_sector) & (ra_count -1 )) - ra_count = page_ra_count; - - bh =sb_find_get_block (sb, sec); -if (!bh || !buffer_uptodate (bh)) { -unsigned int i; - -for (i =0 ; i < ra_count; i++) -sb_breadahead (sb, (sector_t)(sec + i)); - } -brelse (bh); -return 0 ; -} -struct exfat_dentry *exfat_get_dentry (struct super_block *sb,struct exfat_chain *p_dir,int entry,struct buffer_head **bh) { +struct exfat_sb_info *sbi =EXFAT_SB (sb); +unsigned int sect_per_clus = sbi->sect_per_clus;unsigned int dentries_per_page =EXFAT_B_TO_DEN (PAGE_SIZE );int off;sector_t sec;
@@ -673,9 +640,18 @@ struct exfat_dentry *exfat_get_dentry(struct super_block *sb,if (exfat_find_location (sb, p_dir, entry, &sec, &off))return NULL ; -if (p_dir->dir !=EXFAT_FREE_CLUSTER && - !(entry & (dentries_per_page -1 ))) -exfat_dir_readahead (sb, sec); +if (sect_per_clus >1 && + (entry & (dentries_per_page -1 )) ==0 ) { +sector_t ra = sec; +blkcnt_t cnt =0 ; +unsigned int ra_count = sect_per_clus; + +/* Not sector aligned with ra_count, resize ra_count to page size */ +if ((sec - sbi->data_start_sector) & (ra_count -1 )) + ra_count =PAGE_SIZE >> sb->s_blocksize_bits; + +exfat_blk_readahead (sb, sec, &ra, &cnt, sec + ra_count -1 ); + } *bh =sb_bread (sb, sec);if (!*bh)
Signature · 1 line
2.43.0
PATCH v2 4/6 exfat: drop redundant sec parameter from exfat_mirror_bh
In reply to Chi ZhilingFrom: Chi Zhiling <[email protected]>
The sector offset can be obtained from bh->b_blocknr, so drop the redundant sec parameter from exfat_mirror_bh(). Also clean up the function to use exfat_update_bh() helper.
No functional changes.
--- fs/exfat/fatent.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
fs/exfat/fatent.c +4 -7
@@ -11,11 +11,11 @@#include "exfat_raw.h" #include "exfat_fs.h" -static int exfat_mirror_bh (struct super_block *sb,sector_t sec, -struct buffer_head *bh) +static int exfat_mirror_bh (struct super_block *sb,struct buffer_head *bh) {struct buffer_head *c_bh;struct exfat_sb_info *sbi =EXFAT_SB (sb); +sector_t sec = bh->b_blocknr;sector_t sec2;int err =0 ;
@@ -25,10 +25,7 @@ static int exfat_mirror_bh(struct super_block *sb, sector_t sec,if (!c_bh)return -ENOMEM ;memcpy (c_bh->b_data, bh->b_data, sb->s_blocksize); -set_buffer_uptodate (c_bh); -mark_buffer_dirty (c_bh); -if (sb->s_flags &SB_SYNCHRONOUS ) - err =sync_dirty_buffer (c_bh); +exfat_update_bh (c_bh, sb->s_flags &SB_SYNCHRONOUS );brelse (c_bh); }
@@ -83,7 +80,7 @@ int exfat_ent_set(struct super_block *sb, unsigned int loc, fat_entry = (__le32 *)&(bh->b_data[off]); *fat_entry =cpu_to_le32 (content);exfat_update_bh (bh, sb->s_flags &SB_SYNCHRONOUS ); -exfat_mirror_bh (sb, sec, bh); +exfat_mirror_bh (sb, bh);brelse (bh);return 0 ; }
Signature · 1 line
2.43.0
PATCH v2 5/6 exfat: optimize exfat_chain_cont_cluster with cached buffer heads
In reply to Chi ZhilingFrom: Chi Zhiling <[email protected]>
When converting files from NO_FAT_CHAIN to FAT_CHAIN format, profiling reveals significant time spent in mark_buffer_dirty() and exfat_mirror_bh() operations. This overhead occurs because each FAT entry modification triggers a full block dirty marking and mirroring operation.
For consecutive clusters that reside in the same block, optimize by caching the buffer head and performing dirty marking only once at the end of the block's modifications.
Performance improvements for converting a 30GB file:
| Cluster Size | Before Patch | After Patch | Speedup | |--------------|--------------|-------------|---------| | 512 bytes | 4.243s | 1.866s | 2.27x | | 4KB | 0.863s | 0.236s | 3.66x | | 32KB | 0.069s | 0.034s | 2.03x | | 256KB | 0.012s | 0.006s | 2.00x |
--- fs/exfat/fatent.c | 49 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-)
fs/exfat/fatent.c +37 -12
@@ -32,6 +32,17 @@ static int exfat_mirror_bh(struct super_block *sb, struct buffer_head *bh)return err; } +static int exfat_end_bh (struct super_block *sb,struct buffer_head *bh) +{ +int err; + +exfat_update_bh (bh, sb->s_flags &SB_SYNCHRONOUS ); + err =exfat_mirror_bh (sb, bh); +brelse (bh); + +return err; +} +static int __exfat_ent_get (struct super_block *sb,unsigned int loc,unsigned int *content,struct buffer_head **last) {
@@ -62,29 +73,40 @@ static int __exfat_ent_get(struct super_block *sb, unsigned int loc,return 0 ; } -int exfat_ent_set (struct super_block *sb,unsigned int loc, -unsigned int content) +static int __exfat_ent_set (struct super_block *sb,unsigned int loc, +unsigned int content,struct buffer_head **cache) { -unsigned int off;sector_t sec;__le32 *fat_entry; -struct buffer_head *bh; +struct buffer_head *bh = cache ? *cache :NULL ; +unsigned int off; sec =FAT_ENT_OFFSET_SECTOR (sb, loc); off =FAT_ENT_OFFSET_BYTE_IN_SECTOR (sb, loc); - bh =sb_bread (sb, sec); -if (!bh) -return -EIO ; +if (!bh || bh->b_blocknr != sec || !buffer_uptodate (bh)) { +if (bh) +exfat_end_bh (sb, bh); + bh =sb_bread (sb, sec); +if (cache) + *cache = bh; +if (unlikely (!bh)) +return -EIO ; + } fat_entry = (__le32 *)&(bh->b_data[off]); *fat_entry =cpu_to_le32 (content); -exfat_update_bh (bh, sb->s_flags &SB_SYNCHRONOUS ); -exfat_mirror_bh (sb, bh); -brelse (bh); +if (!cache) +exfat_end_bh (sb, bh);return 0 ; } +int exfat_ent_set (struct super_block *sb,unsigned int loc, +unsigned int content) +{ +return __exfat_ent_set (sb, loc, content,NULL ); +} +/* * Caller must release the buffer_head if no error return. */
@@ -170,6 +192,7 @@ int exfat_blk_readahead(struct super_block *sb, sector_t sec,int exfat_chain_cont_cluster (struct super_block *sb,unsigned int chain,unsigned int len) { +struct buffer_head *bh =NULL ;sector_t sec, end, ra;blkcnt_t ra_cnt =0 ;
@@ -183,14 +206,16 @@ int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain, sec =FAT_ENT_OFFSET_SECTOR (sb, chain);exfat_blk_readahead (sb, sec, &ra, &ra_cnt, end); -if (exfat_ent_set (sb, chain, chain +1 )) +if (__exfat_ent_set (sb, chain, chain +1 , &bh))return -EIO ; chain++; len--; } -if (exfat_ent_set (sb, chain,EXFAT_EOF_CLUSTER )) +if (__exfat_ent_set (sb, chain,EXFAT_EOF_CLUSTER , &bh))return -EIO ; + +exfat_end_bh (sb, bh);return 0 ; }
Signature · 1 line
2.43.0
PATCH v2 6/6 exfat: fix error handling for FAT table operations
In reply to Chi ZhilingFrom: Chi Zhiling <[email protected]>
Fix three error handling issues in FAT table operations:
1. Fix exfat_update_bh() to properly return errors from sync_dirty_buffer
2. Fix exfat_end_bh() to properly return errors from exfat_update_bh() and exfat_mirror_bh()
3. Fix ignored return values from exfat_chain_cont_cluster() in inode.c and namei.c
These fixes ensure that FAT table write errors are properly propagated to the caller instead of being silently ignored.
--- fs/exfat/exfat_fs.h | 2 +- fs/exfat/fatent.c | 8 ++++---- fs/exfat/inode.c | 5 +++-- fs/exfat/misc.c | 8 ++++++-- fs/exfat/namei.c | 3 ++- 5 files changed, 16 insertions(+), 10 deletions(-)
fs/exfat/exfat_fs.h +1 -1
@@ -584,7 +584,7 @@ void exfat_set_entry_time(struct exfat_sb_info *sbi, struct timespec64 *ts, u8 *tz, __le16 *time, __le16 *date, u8 *time_cs);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 buffer_head *bh,int sync); +int exfat_update_bh (struct buffer_head *bh,int sync);int exfat_update_bhs (struct buffer_head **bhs,int nr_bhs,int sync);void exfat_chain_set (struct exfat_chain *ec,unsigned int dir,unsigned int size,unsigned char flags);
fs/exfat/fatent.c +4 -4
@@ -25,7 +25,7 @@ static int exfat_mirror_bh(struct super_block *sb, struct buffer_head *bh)if (!c_bh)return -ENOMEM ;memcpy (c_bh->b_data, bh->b_data, sb->s_blocksize); -exfat_update_bh (c_bh, sb->s_flags &SB_SYNCHRONOUS ); + err =exfat_update_bh (c_bh, sb->s_flags &SB_SYNCHRONOUS );brelse (c_bh); }
@@ -36,10 +36,10 @@ static int exfat_end_bh(struct super_block *sb, struct buffer_head *bh) {int err; -exfat_update_bh (bh, sb->s_flags &SB_SYNCHRONOUS ); - err =exfat_mirror_bh (sb, bh); + err =exfat_update_bh (bh, sb->s_flags &SB_SYNCHRONOUS ); +if (!err) + err =exfat_mirror_bh (sb, bh);brelse (bh); -return err; }
fs/exfat/inode.c +3 -2
@@ -204,8 +204,9 @@ static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,* so fat-chain should be synced with * alloc-bitmap */ -exfat_chain_cont_cluster (sb, ei->start_clu, - num_clusters); +if (exfat_chain_cont_cluster (sb, ei->start_clu, + num_clusters)) +return -EIO ; ei->flags =ALLOC_FAT_CHAIN ; }if (new_clu.flags ==ALLOC_FAT_CHAIN )
fs/exfat/misc.c +6 -2
@@ -161,13 +161,17 @@ u32 exfat_calc_chksum32(void *data, int len, u32 chksum, int type)return chksum; } -void exfat_update_bh (struct buffer_head *bh,int sync) +int exfat_update_bh (struct buffer_head *bh,int sync) { +int err =0 ; +set_buffer_uptodate (bh);mark_buffer_dirty (bh);if (sync) -sync_dirty_buffer (bh); + err =sync_dirty_buffer (bh); + +return err; }int exfat_update_bhs (struct buffer_head **bhs,int nr_bhs,int sync)
fs/exfat/namei.c +2 -1
@@ -365,7 +365,8 @@ int exfat_find_empty_entry(struct inode *inode,/* no-fat-chain bit is disabled, * so fat-chain should be synced with alloc-bitmap */ -exfat_chain_cont_cluster (sb, p_dir->dir, p_dir->size); +if (exfat_chain_cont_cluster (sb, p_dir->dir, p_dir->size)) +return -EIO ; p_dir->flags =ALLOC_FAT_CHAIN ; hint_femp.cur.flags =ALLOC_FAT_CHAIN ; }
Signature · 1 line
2.43.0
Quoted 24 lines · Chi Zhiling
From: Chi Zhiling <[email protected]>
This series improves the performance when converting files from
NO_FAT_CHAINtoFAT_CHAINformat (which happens when a file cannot allocate contiguous clusters). It also adds proper error handling for FAT table write operations.- Patch 1: Add block readahead for FAT blocks during conversion
- Patch 2-3: Refactor existing readahead code to use the new helper
- Patch 4: Remove redundant sec parameter fromexfat_mirror_bh
- Patch 5: Cache buffer heads to reducemark_buffer_dirtyoverhead
- Patch 6: Fix error handling to propagate FAT write errors to callersPerformance improvements for converting a 30GB file:
| Cluster Size | Before | After | Speedup | |--------------|--------|--------|---------| | 512 bytes | 47.667s| 1.866s | 25.5x | | 4KB | 6.436s | 0.236s | 27.3x | | 32KB | 0.758s | 0.034s | 22.3x | | 256KB | 0.117s | 0.006s | 19.5x |
Quoted 7 lines
Chi Zhiling (6):
exfat: add block readahead inexfat_chain_cont_cluster
exfat: use readahead helper inexfat_allocate_bitmap
exfat: use readahead helper inexfat_get_dentry
exfat: drop redundant sec parameter fromexfat_mirror_bh
exfat: optimizeexfat_chain_cont_clusterwith cached buffer heads
exfat: fix error handling for FAT table operations
Applied them to #dev.
Thanks!