Skip to content

Commit 6ed88c9

Browse files
Chi Zhilingnamjaejeon
authored andcommitted
exfat: add block readahead in exfat_chain_cont_cluster
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 | Signed-off-by: Chi Zhiling <chizhiling@kylinos.cn> Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
1 parent bf17979 commit 6ed88c9

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

fs/exfat/exfat_fs.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/ratelimit.h>
1111
#include <linux/nls.h>
1212
#include <linux/blkdev.h>
13+
#include <linux/backing-dev.h>
1314
#include <uapi/linux/exfat.h>
1415

1516
#define EXFAT_ROOT_INO 1
@@ -79,6 +80,10 @@ enum {
7980
#define EXFAT_HINT_NONE -1
8081
#define EXFAT_MIN_SUBDIR 2
8182

83+
#define EXFAT_BLK_RA_SIZE(sb) \
84+
(min_t(blkcnt_t, (sb)->s_bdi->ra_pages, (sb)->s_bdi->io_pages) \
85+
<< (PAGE_SHIFT - (sb)->s_blocksize_bits))
86+
8287
/*
8388
* helpers for cluster size to byte conversion.
8489
*/
@@ -117,9 +122,9 @@ enum {
117122
#define FAT_ENT_SIZE (4)
118123
#define FAT_ENT_SIZE_BITS (2)
119124
#define FAT_ENT_OFFSET_SECTOR(sb, loc) (EXFAT_SB(sb)->FAT1_start_sector + \
120-
(((u64)loc << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits))
125+
(((u64)(loc) << FAT_ENT_SIZE_BITS) >> sb->s_blocksize_bits))
121126
#define FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc) \
122-
((loc << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1))
127+
(((loc) << FAT_ENT_SIZE_BITS) & (sb->s_blocksize - 1))
123128

124129
/*
125130
* helpers for bitmap.
@@ -448,6 +453,8 @@ int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
448453
unsigned int *ret_clu);
449454
int exfat_count_num_clusters(struct super_block *sb,
450455
struct exfat_chain *p_chain, unsigned int *ret_count);
456+
int exfat_blk_readahead(struct super_block *sb, sector_t sec,
457+
sector_t *ra, blkcnt_t *ra_cnt, sector_t end);
451458

452459
/* balloc.c */
453460
int exfat_load_bitmap(struct super_block *sb);

fs/exfat/fatent.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,50 @@ int exfat_ent_get(struct super_block *sb, unsigned int loc,
142142
return -EIO;
143143
}
144144

145+
int exfat_blk_readahead(struct super_block *sb, sector_t sec,
146+
sector_t *ra, blkcnt_t *ra_cnt, sector_t end)
147+
{
148+
struct blk_plug plug;
149+
150+
if (sec < *ra)
151+
return 0;
152+
153+
*ra += *ra_cnt;
154+
155+
/* No blocks left (or only the last block), skip readahead. */
156+
if (*ra >= end)
157+
return 0;
158+
159+
*ra_cnt = min(end - *ra + 1, EXFAT_BLK_RA_SIZE(sb));
160+
if (*ra_cnt == 0) {
161+
/* Move 'ra' to the end to disable readahead. */
162+
*ra = end;
163+
return 0;
164+
}
165+
166+
blk_start_plug(&plug);
167+
for (unsigned int i = 0; i < *ra_cnt; i++)
168+
sb_breadahead(sb, *ra + i);
169+
blk_finish_plug(&plug);
170+
return 0;
171+
}
172+
145173
int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
146174
unsigned int len)
147175
{
176+
sector_t sec, end, ra;
177+
blkcnt_t ra_cnt = 0;
178+
148179
if (!len)
149180
return 0;
150181

182+
ra = FAT_ENT_OFFSET_SECTOR(sb, chain);
183+
end = FAT_ENT_OFFSET_SECTOR(sb, chain + len - 1);
184+
151185
while (len > 1) {
186+
sec = FAT_ENT_OFFSET_SECTOR(sb, chain);
187+
exfat_blk_readahead(sb, sec, &ra, &ra_cnt, end);
188+
152189
if (exfat_ent_set(sb, chain, chain + 1))
153190
return -EIO;
154191
chain++;

0 commit comments

Comments
 (0)