Skip to content

Commit 30269ce

Browse files
chaseyudhavale
authored andcommitted
BACKPORT: 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 Bug: 407861253 (cherry picked from commit df0ce6c) Change-Id: I0b52529cebce61f598dda33244ae257d5f5e1320 [Chao: resolve the conflict] 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> Signed-off-by: Chao Yu <chao@kernel.org>
1 parent 74a4608 commit 30269ce

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
@@ -16,3 +16,11 @@ Description: Control strategy of sync decompression:
1616
readahead on atomic contexts only.
1717
- 1 (force on): enable for readpage and readahead.
1818
- 2 (force off): disable for all situations.
19+
20+
What: /sys/fs/erofs/<disk>/dir_ra_bytes
21+
Date: July 2025
22+
Contact: "Chao Yu" <chao@kernel.org>
23+
Description: Used to set or show readahead bytes during readdir(), by
24+
default the value is 16384.
25+
26+
- 0: disable readahead.

fs/erofs/dir.c

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

@@ -58,6 +62,16 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)
5862
struct erofs_dirent *de;
5963
unsigned int nameoff, maxsize;
6064

65+
/* readahead blocks to enhance performance for large directories */
66+
if (ra_pages) {
67+
pgoff_t idx = DIV_ROUND_UP_POW2(ctx->pos, PAGE_SIZE);
68+
pgoff_t pages = min(nr_pages - idx, ra_pages);
69+
70+
if (pages > 1 && !ra_has_index(ra, idx))
71+
page_cache_sync_readahead(dir->i_mapping, ra,
72+
f, idx, pages);
73+
}
74+
6175
de = erofs_bread(&buf, dbstart, EROFS_KMAP);
6276
if (IS_ERR(de)) {
6377
erofs_err(sb, "fail to readdir of logical block %u of nid %llu",

fs/erofs/internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ struct erofs_sb_info {
166166
/* sysfs support */
167167
struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
168168
struct completion s_kobj_unregister;
169+
erofs_off_t dir_ra_bytes;
169170

170171
/* fscache support */
171172
struct fscache_volume *volume;
@@ -252,6 +253,9 @@ EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
252253
#define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
253254
#define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
254255

256+
/* default readahead size of directories */
257+
#define EROFS_DIR_RA_BYTES 16384
258+
255259
struct erofs_inode {
256260
erofs_nid_t nid;
257261

fs/erofs/super.c

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

745+
sbi->dir_ra_bytes = EROFS_DIR_RA_BYTES;
745746
erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid);
746747
return 0;
747748
}

fs/erofs/sysfs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ static struct erofs_attr erofs_attr_##_name = { \
5858
#ifdef CONFIG_EROFS_FS_ZIP
5959
EROFS_ATTR_RW_UI(sync_decompress, erofs_mount_opts);
6060
#endif
61+
EROFS_ATTR_RW_UI(dir_ra_bytes, erofs_sb_info);
6162

6263
static struct attribute *erofs_attrs[] = {
6364
#ifdef CONFIG_EROFS_FS_ZIP
6465
ATTR_LIST(sync_decompress),
6566
#endif
67+
ATTR_LIST(dir_ra_bytes),
6668
NULL,
6769
};
6870
ATTRIBUTE_GROUPS(erofs);

0 commit comments

Comments
 (0)