Skip to content

Commit df0ce6c

Browse files
chaseyuhsiangkao
authored andcommitted
erofs: support to readahead dirent blocks in erofs_readdir()
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>
1 parent 4140913 commit df0ce6c

5 files changed

Lines changed: 29 additions & 0 deletions

File tree

Documentation/ABI/testing/sysfs-fs-erofs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ Description: Used to set or show hardware accelerators in effect
3535
and multiple accelerators are separated by '\n'.
3636
Supported accelerator(s): qat_deflate.
3737
Disable all accelerators with an empty string (echo > accel).
38+
39+
What: /sys/fs/erofs/<disk>/dir_ra_bytes
40+
Date: July 2025
41+
Contact: "Chao Yu" <chao@kernel.org>
42+
Description: Used to set or show readahead bytes during readdir(), by
43+
default the value is 16384.
44+
45+
- 0: disable readahead.

fs/erofs/dir.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
4848
struct inode *dir = file_inode(f);
4949
struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
5050
struct super_block *sb = dir->i_sb;
51+
struct file_ra_state *ra = &f->f_ra;
5152
unsigned long bsz = sb->s_blocksize;
5253
unsigned int ofs = erofs_blkoff(sb, ctx->pos);
54+
pgoff_t ra_pages = DIV_ROUND_UP_POW2(
55+
EROFS_I_SB(dir)->dir_ra_bytes, PAGE_SIZE);
56+
pgoff_t nr_pages = DIV_ROUND_UP_POW2(dir->i_size, PAGE_SIZE);
5357
int err = 0;
5458
bool initial = true;
5559

@@ -64,6 +68,16 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
6468
break;
6569
}
6670

71+
/* readahead blocks to enhance performance for large directories */
72+
if (ra_pages) {
73+
pgoff_t idx = DIV_ROUND_UP_POW2(ctx->pos, PAGE_SIZE);
74+
pgoff_t pages = min(nr_pages - idx, ra_pages);
75+
76+
if (pages > 1 && !ra_has_index(ra, idx))
77+
page_cache_sync_readahead(dir->i_mapping, ra,
78+
f, idx, pages);
79+
}
80+
6781
de = erofs_bread(&buf, dbstart, true);
6882
if (IS_ERR(de)) {
6983
erofs_err(sb, "failed to readdir of logical block %llu of nid %llu",

fs/erofs/internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ struct erofs_sb_info {
159159
/* sysfs support */
160160
struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
161161
struct completion s_kobj_unregister;
162+
erofs_off_t dir_ra_bytes;
162163

163164
/* fscache support */
164165
struct fscache_volume *volume;
@@ -259,6 +260,9 @@ static inline u64 erofs_nid_to_ino64(struct erofs_sb_info *sbi, erofs_nid_t nid)
259260
#define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
260261
#define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
261262

263+
/* default readahead size of directories */
264+
#define EROFS_DIR_RA_BYTES 16384
265+
262266
struct erofs_inode {
263267
erofs_nid_t nid;
264268

fs/erofs/super.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
731731
if (err)
732732
return err;
733733

734+
sbi->dir_ra_bytes = EROFS_DIR_RA_BYTES;
734735
erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid);
735736
return 0;
736737
}

fs/erofs/sysfs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ EROFS_ATTR_FUNC(drop_caches, 0200);
6565
#ifdef CONFIG_EROFS_FS_ZIP_ACCEL
6666
EROFS_ATTR_FUNC(accel, 0644);
6767
#endif
68+
EROFS_ATTR_RW_UI(dir_ra_bytes, erofs_sb_info);
6869

6970
static struct attribute *erofs_sb_attrs[] = {
7071
#ifdef CONFIG_EROFS_FS_ZIP
7172
ATTR_LIST(sync_decompress),
7273
ATTR_LIST(drop_caches),
7374
#endif
75+
ATTR_LIST(dir_ra_bytes),
7476
NULL,
7577
};
7678
ATTRIBUTE_GROUPS(erofs_sb);

0 commit comments

Comments
 (0)