erofs: support to readahead dirent blocks in erofs_readdir()
Chao Yu
maintainerPATCH v3 1/1 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
---
v3:
- add EROFS prefix for macro
- update new sysfs interface to 1) use bytes instead of pages
2) remove upper boundary limitation
- fix bug of pageidx calculation
Documentation/ABI/testing/sysfs-fs-erofs | 8 ++++++++ fs/erofs/dir.c | 13 +++++++++++++ fs/erofs/internal.h | 4 ++++ fs/erofs/super.c | 2 ++ fs/erofs/sysfs.c | 2 ++ 5 files changed, 29 insertions(+)
Documentation/ABI/testing/sysfs-fs-erofs +8 -0
@@ -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" <[email protected]> +Description: Used to set or show readahead bytes during readdir(), by + default the value is 16384. + + - 0: disable readahead.
fs/erofs/dir.c +13 -0
@@ -47,8 +47,10 @@ 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); +unsigned long nr_pages =DIV_ROUND_UP_POW2 (dir->i_size,PAGE_SIZE );int err =0 ;bool initial = true;
@@ -63,6 +65,17 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)break ; } +/* readahead blocks to enhance performance in large directory */ +if (EROFS_I_SB (dir)->dir_ra_bytes) { +unsigned long idx =DIV_ROUND_UP (ctx->pos,PAGE_SIZE ); +pgoff_t ra_pages =DIV_ROUND_UP ( +EROFS_I_SB (dir)->dir_ra_bytes,PAGE_SIZE ); + +if (nr_pages - idx >1 && !ra_has_index (ra, idx)) +page_cache_sync_readahead (dir->i_mapping, ra, + f, idx,min (nr_pages - idx, ra_pages)); + } + de =erofs_bread (&buf, dbstart, true);if (IS_ERR (de)) { erofs_err(sb, "failed to readdir of logical block %llu of nid %llu",
fs/erofs/internal.h +4 -0
@@ -157,6 +157,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;
@@ -238,6 +239,9 @@ EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)#define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)#define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2) +/* default readahead size of directory */ +#define EROFS_DIR_RA_BYTES 16384 +struct erofs_inode {erofs_nid_t nid;
fs/erofs/super.c +2 -0
@@ -715,6 +715,8 @@ 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 ; }
fs/erofs/sysfs.c +2 -0
@@ -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);
Signature · 1 line
2.49.0
Hi Chao,
Quoted 50 lines · Chao Yu
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.562Meanwhile, 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 0Signed-off-by Chao Yu <[email protected]>---
v3:
- add EROFS prefix for macro
- update new sysfs interface to 1) use bytes instead of pages
2) remove upper boundary limitation
- fix bug of pageidx calculationDocumentation/ABI/testing/sysfs-fs-erofs | 8 ++++++++ fs/erofs/dir.c | 13 +++++++++++++ fs/erofs/internal.h | 4 ++++ fs/erofs/super.c | 2 ++ fs/erofs/sysfs.c | 2 ++ 5 files changed, 29 insertions(+)Documentation/ABI/testing/sysfs-fs-erofs +8
-0@@ -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" <[email protected]> +Description: Used to set or show readahead bytes during readdir(), by + default the value is 16384. + + - 0: disable readahead.fs/erofs/dir.c +0
-0
fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 53
Quoted 7 lines
fs/erofs/dir.c +2
-0@@ -47,8 +47,10 @@ 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); +unsigned long nr_pages =DIV_ROUND_UP_POW2 (dir->i_size,PAGE_SIZE );
pgoff_t ra_pages = PAGE_ALIGN(EROFS_SB(dir)->dir_ra_bytes);
fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 69
Quoted 9 lines
fs/erofs/dir.c +2
-0int err =0 ;bool initial = true;@@ -63,6 +65,17 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)break ; } +/* readahead blocks to enhance performance in large directory */ +if (EROFS_I_SB (dir)->dir_ra_bytes) {
if (ra_pages) {fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 72
Quoted 3 lines
fs/erofs/dir.c +3
-0+unsigned long idx =DIV_ROUND_UP (ctx->pos,PAGE_SIZE ); +pgoff_t ra_pages =DIV_ROUND_UP ( +EROFS_I_SB (dir)->dir_ra_bytes,PAGE_SIZE );
pgoff_t idx = PAGE_ALIGN(ctx->pos); pgoff_t pages = min(nr_pages - idx, ra_pages);
fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 74
Quoted 2 lines
fs/erofs/dir.c +2
-0+ +if (nr_pages - idx >1 && !ra_has_index (ra, idx))
if (pages > 1 && !ra_has_index(ra, idx)) page_cache_sync_readahead(dir->i_mapping, ra, f, idx, pages)?
Quoted 20 lines
fs/erofs/dir.c +4
-0+page_cache_sync_readahead (dir->i_mapping, ra, + f, idx,min (nr_pages - idx, ra_pages)); + } + de =erofs_bread (&buf, dbstart, true);if (IS_ERR (de)) { erofs_err(sb, "failed to readdir of logical block %llu of nid %llu",fs/erofs/internal.h +1
-0@@ -157,6 +157,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;
fs/erofs/internal.h EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER) · line 242
Quoted 4 lines
fs/erofs/internal.h +1
-0@@ -238,6 +239,9 @@ EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)#define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)#define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2) +/* default readahead size of directory */
/* default readahead size of directories */
Otherwise it looks good to me.
Thanks,
Gao Xiang
Quoted 1 line
fs/erofs/internal.h +1
-0+#define EROFS_DIR_RA_BYTES 16384
Chao Yu
maintainerHi Xiang,
Quoted 56 lines · Gao Xiang
Hi Chao,
Quoted 53 lines · Chao Yu
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.562Meanwhile, 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 0Signed-off-by Chao Yu <[email protected]>---
v3:
- add EROFS prefix for macro
- update new sysfs interface to 1) use bytes instead of pages
2) remove upper boundary limitation
- fix bug of pageidx calculationDocumentation/ABI/testing/sysfs-fs-erofs| 8 ++++++++ fs/erofs/dir.c | 13 +++++++++++++ fs/erofs/internal.h | 4 ++++ fs/erofs/super.c | 2 ++ fs/erofs/sysfs.c | 2 ++5 files changed, 29 insertions(+)Documentation/ABI/testing/sysfs-fs-erofs +0
-0@@ -35,3 +35,11 @@ Description: Used to set or show hardwareaccelerators in effect
and multiple accelerators are separated by '\n'. Supported accelerator(s):qat_deflate.
Disable all accelerators with an empty string (echo > accel).Documentation/ABI/testing/sysfs-fs-erofs +8
-0+ +What: /sys/fs/erofs/<disk>/dir_ra_bytes +Date: July 2025 +Contact: "Chao Yu" <[email protected]> +Description: Used to set or show readahead bytes during readdir(), by + default the value is 16384. + + - 0: disable readahead.fs/erofs/dir.c +0
-0@@ -47,8 +47,10 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx) struct inode *dir = file_inode(f);
Quoted 8 lines
fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 53
Quoted 6 lines
fs/erofs/dir.c +2
-0struct 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); + unsigned long nr_pages = DIV_ROUND_UP_POW2(dir->i_size, PAGE_SIZE);pgoff_t
ra_pages= PAGE_ALIGN(EROFS_SB(dir)->dir_ra_bytes);
Do you mean?
pgoff_t ra_pages = PAGE_ALIGN(EROFS_SB(dir)->dir_ra_bytes) >> PAGE_SHIFT?
Quoted 11 lines
Quoted 10 lines
fs/erofs/dir.c +2
-0int err = 0; bool initial = true;@@ -63,6 +65,17 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx) break; } + /* readahead blocks to enhance performance in large directory */ + if (EROFS_I_SB(dir)->dir_ra_bytes) { if (ra_pages) {
Quoted 3 lines
fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 72
Quoted 3 lines
fs/erofs/dir.c +3
-0+ unsigned long idx = DIV_ROUND_UP(ctx->pos, PAGE_SIZE); + pgoff_t ra_pages = DIV_ROUND_UP( + EROFS_I_SB(dir)->dir_ra_bytes, PAGE_SIZE);
I put calculation here because if the value is zero, we don't need unnecessary calculation above, anyway, will update as you suggested, but let me know if you have any other concerns. :)
Quoted 2 lines
pgoff_tidx =PAGE_ALIGN(ctx->pos);
Ditto,
Thanks,
Quoted 42 lines
pgoff_tpages =min(nr_pages - idx, ra_pages);fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 74
Quoted 2 lines
fs/erofs/dir.c +2
-0+ + if (nr_pages - idx > 1 && !ra_has_index(ra, idx))if (pages > 1 && !ra_has_index(ra, idx))
page_cache_sync_readahead(dir->i_mapping, ra, f, idx, pages)?Quoted 19 lines
fs/erofs/dir.c +4
-0+ page_cache_sync_readahead(dir->i_mapping, ra, + f, idx, min(nr_pages - idx, ra_pages)); + } + de = erofs_bread(&buf, dbstart, true); if (IS_ERR(de)) {erofs_err(sb, "failed to readdir of logical block %llu of nid %llu",
fs/erofs/internal.h +0
-0@@ -157,6 +157,7 @@ struct erofs_sb_info { /* sysfs support */struct kobject s_kobj; /* /sys/fs/erofs/<devname> */ struct completion
s_kobj_unregister;
+erofs_off_tdir_ra_bytes;
/* fscache support */
structfscache_volume*volume;fs/erofs/internal.h EROFS_FEATURE_FUNCS(xattr_filter, compat, · line 242
Quoted 4 lines
fs/erofs/internal.h +1
-0@@ -238,6 +239,9 @@ EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER) #define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1) #define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2) +/* default readahead size of directory */ /* default readahead size of directories */
Otherwise it looks good to me.
Thanks,
Gao XiangQuoted 1 line
fs/erofs/internal.h +1
-0+#define EROFS_DIR_RA_BYTES 16384
Quoted 66 lines · Chao Yu
Hi Xiang,
Quoted 54 lines · Gao Xiang
Hi Chao,
Quoted 51 lines · Chao Yu
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.562Meanwhile, 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 0Signed-off-by Chao Yu <[email protected]>---
v3:
- add EROFS prefix for macro
- update new sysfs interface to 1) use bytes instead of pages
2) remove upper boundary limitation
- fix bug of pageidx calculationDocumentation/ABI/testing/sysfs-fs-erofs| 8 ++++++++ fs/erofs/dir.c | 13 +++++++++++++ fs/erofs/internal.h | 4 ++++ fs/erofs/super.c | 2 ++ fs/erofs/sysfs.c | 2 ++5 files changed, 29 insertions(+)Documentation/ABI/testing/sysfs-fs-erofs +0
-0@@ -35,3 +35,11 @@ Description: Used to set or show hardware accelerators in effectand multiple accelerators are separated by '\n'. Supported accelerator(s):
qat_deflate.
Disable all accelerators with an empty string (echo > accel).Documentation/ABI/testing/sysfs-fs-erofs +8
-0+ +What: /sys/fs/erofs/<disk>/dir_ra_bytes +Date: July 2025 +Contact: "Chao Yu" <[email protected]> +Description: Used to set or show readahead bytes during readdir(), by + default the value is 16384. + + - 0: disable readahead.fs/erofs/dir.c +0
-0@@ -47,8 +47,10 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx) struct inode *dir = file_inode(f);Quoted 8 lines
fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 53
Quoted 6 lines
fs/erofs/dir.c +2
-0struct 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); + unsigned long nr_pages = DIV_ROUND_UP_POW2(dir->i_size, PAGE_SIZE);pgoff_t
ra_pages= PAGE_ALIGN(EROFS_SB(dir)->dir_ra_bytes);
Quoted 3 lines
Do you mean?
pgoff_tra_pages= PAGE_ALIGN(EROFS_SB(dir)->dir_ra_bytes) >>PAGE_SHIFT?
That seems more complicated, sigh, I think the original DIV_ROUND_UP_POW2() is better.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
fs/erofs/dir.c +2
-0int err = 0; bool initial = true;@@ -63,6 +65,17 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx) break; } + /* readahead blocks to enhance performance in large directory */ + if (EROFS_I_SB(dir)->dir_ra_bytes) { if (ra_pages) {
Quoted 5 lines
Quoted 3 lines
fs/erofs/dir.c static int erofs_readdir(struct file *f, struct dir_context *ctx) · line 72
Quoted 3 lines
fs/erofs/dir.c +3
-0+ unsigned long idx = DIV_ROUND_UP(ctx->pos, PAGE_SIZE); + pgoff_t ra_pages = DIV_ROUND_UP( + EROFS_I_SB(dir)->dir_ra_bytes, PAGE_SIZE);I put calculation here because if the value is zero, we don't need unnecessary calculation above, anyway, will update as you suggested, but let me know if you have any other concerns. :)
If only shift calculatiion is needed, I guess it
should not have much overhead, like:
if (ra_pages) {
pgoff_t idx = DIV_ROUND_UP_POW2(ctx->pos, PAGE_SIZE);
pgoff_t pages = min(nr_pages - idx, ra_pages);
...Thanks,
Gao Xiang