]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
erofs: support to readahead dirent blocks in erofs_readdir()
authorChao Yu <chao@kernel.org>
Mon, 21 Jul 2025 02:13:52 +0000 (10:13 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Thu, 24 Jul 2025 11:44:08 +0000 (19:44 +0800)
This patch supports to readahead more blocks in erofs_readdir(), it can
enhance readdir performance in large direcotry.

readdir test in a large directory which contains 12000 sub-files.

files_per_second
Before: 926385.54
After: 2380435.562

Meanwhile, let's introduces a new sysfs entry to control readahead
bytes to provide more flexible policy for readahead of readdir().
- location: /sys/fs/erofs/<disk>/dir_ra_bytes
- default value: 16384
- disable readahead: set the value to 0

Signed-off-by: Chao Yu <chao@kernel.org>
Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20250721021352.2495371-1-chao@kernel.org
[ Gao Xiang: minor styling adjustment. ]
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Documentation/ABI/testing/sysfs-fs-erofs
fs/erofs/dir.c
fs/erofs/internal.h
fs/erofs/super.c
fs/erofs/sysfs.c

index ad6d1a3ccd4e062c96fa74481521a9993842da69..76d9808ed581436d2a01e19361c7d01366da9b40 100644 (file)
@@ -35,3 +35,11 @@ Description: Used to set or show hardware accelerators in effect
                and multiple accelerators are separated by '\n'.
                Supported accelerator(s): qat_deflate.
                Disable all accelerators with an empty string (echo > accel).
+
+What:          /sys/fs/erofs/<disk>/dir_ra_bytes
+Date:          July 2025
+Contact:       "Chao Yu" <chao@kernel.org>
+Description:   Used to set or show readahead bytes during readdir(), by
+               default the value is 16384.
+
+               - 0: disable readahead.
index d1c6cd1a45e877b88e552faaa40061c76827007a..debf469ad6bd56e92af9f7fd0c0874ca5f4fa05c 100644 (file)
@@ -48,8 +48,12 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
        struct inode *dir = file_inode(f);
        struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
        struct super_block *sb = dir->i_sb;
+       struct file_ra_state *ra = &f->f_ra;
        unsigned long bsz = sb->s_blocksize;
        unsigned int ofs = erofs_blkoff(sb, ctx->pos);
+       pgoff_t ra_pages = DIV_ROUND_UP_POW2(
+                       EROFS_I_SB(dir)->dir_ra_bytes, PAGE_SIZE);
+       pgoff_t nr_pages = DIV_ROUND_UP_POW2(dir->i_size, PAGE_SIZE);
        int err = 0;
        bool initial = true;
 
@@ -64,6 +68,16 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
                        break;
                }
 
+               /* readahead blocks to enhance performance for large directories */
+               if (ra_pages) {
+                       pgoff_t idx = DIV_ROUND_UP_POW2(ctx->pos, PAGE_SIZE);
+                       pgoff_t pages = min(nr_pages - idx, ra_pages);
+
+                       if (pages > 1 && !ra_has_index(ra, idx))
+                               page_cache_sync_readahead(dir->i_mapping, ra,
+                                                         f, idx, pages);
+               }
+
                de = erofs_bread(&buf, dbstart, true);
                if (IS_ERR(de)) {
                        erofs_err(sb, "failed to readdir of logical block %llu of nid %llu",
index 26f9aa57ff3cabefda9847406116f5e7cd294f6f..4ccc5f0ee8dfb99b31b36ab83a626c9f22aeacc9 100644 (file)
@@ -159,6 +159,7 @@ struct erofs_sb_info {
        /* sysfs support */
        struct kobject s_kobj;          /* /sys/fs/erofs/<devname> */
        struct completion s_kobj_unregister;
+       erofs_off_t dir_ra_bytes;
 
        /* fscache support */
        struct fscache_volume *volume;
@@ -259,6 +260,9 @@ static inline u64 erofs_nid_to_ino64(struct erofs_sb_info *sbi, erofs_nid_t nid)
 #define EROFS_I_BL_XATTR_BIT   (BITS_PER_LONG - 1)
 #define EROFS_I_BL_Z_BIT       (BITS_PER_LONG - 2)
 
+/* default readahead size of directories */
+#define EROFS_DIR_RA_BYTES     16384
+
 struct erofs_inode {
        erofs_nid_t nid;
 
index c7b9b784dc3daf8ea67de3926e4a8297ee6c63b0..e1020aa607714ebaa187b2d763df36004e950ba1 100644 (file)
@@ -731,6 +731,7 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
        if (err)
                return err;
 
+       sbi->dir_ra_bytes = EROFS_DIR_RA_BYTES;
        erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid);
        return 0;
 }
index a9fe35ba0906ffa8f2bff48cb18dc61a8ec0f76f..1e0658a1d95b9acad0c665d147d572c2cc10d36e 100644 (file)
@@ -65,12 +65,14 @@ EROFS_ATTR_FUNC(drop_caches, 0200);
 #ifdef CONFIG_EROFS_FS_ZIP_ACCEL
 EROFS_ATTR_FUNC(accel, 0644);
 #endif
+EROFS_ATTR_RW_UI(dir_ra_bytes, erofs_sb_info);
 
 static struct attribute *erofs_sb_attrs[] = {
 #ifdef CONFIG_EROFS_FS_ZIP
        ATTR_LIST(sync_decompress),
        ATTR_LIST(drop_caches),
 #endif
+       ATTR_LIST(dir_ra_bytes),
        NULL,
 };
 ATTRIBUTE_GROUPS(erofs_sb);