]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
exfat: add block readahead in exfat_chain_cont_cluster
authorChi Zhiling <chizhiling@kylinos.cn>
Tue, 3 Mar 2026 03:14:04 +0000 (11:14 +0800)
committerNamjae Jeon <linkinjeon@kernel.org>
Thu, 5 Mar 2026 12:08:21 +0000 (21:08 +0900)
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>
fs/exfat/exfat_fs.h
fs/exfat/fatent.c

index 2dbed5f8ec2674acf69eac7943a75b6fd8a39de5..090f25d1a418f68656ab365308f1f9a507db271f 100644 (file)
@@ -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);
index f87576ca70329ddf4dec76cf84858d9b200bead7..9a4143f3fc0ca57b725d047d34a3ca7153e1f7dd 100644 (file)
@@ -142,13 +142,50 @@ err:
        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++;