Skip to contentSkip to conversation

erofs: support to readahead dirent blocks in erofs_readdir()

PATCH Superseded Chao Yu 2 participants 4 messages Jul 14, 2025 EROFS file system

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Chao Yu

author maintainer
#0 lore raw

PATCH v2 2/2 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 page count of readahead to provide more flexible policy for readahead of readdir().
- location: /sys/fs/erofs/<disk>/dir_ra_pages
- default value: 4
- range: [0, 128]
- disable readahead: set the value to 0

---
v2:
- introduce sysfs node to control page count of readahead during readdir().

 Documentation/ABI/testing/sysfs-fs-erofs | 7 +++++++
 fs/erofs/dir.c                           | 9 +++++++++
 fs/erofs/internal.h                      | 5 +++++
 fs/erofs/super.c                         | 2 ++
 fs/erofs/sysfs.c                         | 5 +++++
 5 files changed, 28 insertions(+)
Documentation/ABI/testing/sysfs-fs-erofs +7 -0
index bf3b6299c15e..500c93484e4c 100644
@@ -35,3 +35,10 @@ 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_pages
+Date:		July 2025
+Contact:	"Chao Yu" <[email protected]>
+Description:	Used to set or show page count of readahead during readdir(),
+		the range of value is [0, 128], by default it is 4, set it to
+		0 to disable readahead.
fs/erofs/dir.c +9 -0
index 3e4b38bec0aa..40f828d5b670 100644
@@ -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,13 @@ 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_pages && nr_pages - dbstart > 1 &&
+		    !ra_has_index(ra, dbstart))
+			page_cache_sync_readahead(dir->i_mapping, ra, f,
+				dbstart, min(nr_pages - dbstart,
+				(pgoff_t)EROFS_I_SB(dir)->dir_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 +5 -0
index 0d19bde8c094..f0e5b4273aa8 100644
@@ -157,6 +157,7 @@ struct erofs_sb_info {
 	/* sysfs support */
 	struct kobject s_kobj;		/* /sys/fs/erofs/<devname> */
 	struct completion s_kobj_unregister;
+	unsigned int dir_ra_pages;
 
 	/* fscache support */
 	struct fscache_volume *volume;
@@ -238,6 +239,10 @@ 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/maximum readahead pages of directory */
+#define DEFAULT_DIR_RA_PAGES	4
+#define MAX_DIR_RA_PAGES	128
+
 struct erofs_inode {
 	erofs_nid_t nid;
 
fs/erofs/super.c +2 -0
index e1e9f06e8342..90e5ec04a768 100644
@@ -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_pages = DEFAULT_DIR_RA_PAGES;
+
 	erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid);
 	return 0;
 }
fs/erofs/sysfs.c +5 -0
index eed8797a193f..3e47084e8730 100644
@@ -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_pages, 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_pages),
 	NULL,
 };
 ATTRIBUTE_GROUPS(erofs_sb);
@@ -171,6 +173,9 @@ static ssize_t erofs_attr_store(struct kobject *kobj, struct attribute *attr,
 		    (t > EROFS_SYNC_DECOMPRESS_FORCE_OFF))
 			return -EINVAL;
 #endif
+		if (!strcmp(a->attr.name, "dir_ra_pages") &&
+		    (t > MAX_DIR_RA_PAGES))
+			return -EINVAL;
 		*(unsigned int *)ptr = t;
 		return len;
 	case attr_pointer_bool:
Signature · 1 line
2.49.0
#1 lore raw

Hi Chao,

Quoted 48 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.562

Meanwhile, let's introduces a new sysfs entry to control page count of readahead to provide more flexible policy for readahead of readdir().
- location: /sys/fs/erofs/<disk>/dir_ra_pages
- default value: 4
- range: [0, 128]
- disable readahead: set the value to 0

---
v2:
- introduce sysfs node to control page count of readahead during readdir().

  Documentation/ABI/testing/sysfs-fs-erofs | 7 +++++++
  fs/erofs/dir.c                           | 9 +++++++++
  fs/erofs/internal.h                      | 5 +++++
  fs/erofs/super.c                         | 2 ++
  fs/erofs/sysfs.c                         | 5 +++++
  5 files changed, 28 insertions(+)
Documentation/ABI/testing/sysfs-fs-erofs +7 -0
index bf3b6299c15e..500c93484e4c 100644
@@ -35,3 +35,10 @@ 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_pages
+Date:		July 2025
+Contact:	"Chao Yu" <[email protected]>
+Description:	Used to set or show page count of readahead during readdir(),
+		the range of value is [0, 128], by default it is 4, set it to
+		0 to disable readahead.
fs/erofs/dir.c +0 -0
index 3e4b38bec0aa..40f828d5b670 100644

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);

why using DIV_ROUND_UP_POW2 rather than DIV_ROUND_UP here?

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 -0
  	int err = 0;
  	bool initial = true;
 
@@ -63,6 +65,13 @@ 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_pages && nr_pages - dbstart > 1 &&

dbstart is a byte-oriented value, so I'm not sure if it works as you expect..

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
+		    !ra_has_index(ra, dbstart))
+			page_cache_sync_readahead(dir->i_mapping, ra, f,
+				dbstart, min(nr_pages - dbstart,

same here.

Quoted 18 lines
fs/erofs/dir.c +2 -0
+				(pgoff_t)EROFS_I_SB(dir)->dir_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
index 0d19bde8c094..f0e5b4273aa8 100644
@@ -157,6 +157,7 @@ struct erofs_sb_info {
  	/* sysfs support */
  	struct kobject s_kobj;		/* /sys/fs/erofs/<devname> */
  	struct completion s_kobj_unregister;
+	unsigned int dir_ra_pages;
 
  	/* fscache support */
  	struct fscache_volume *volume;

fs/erofs/internal.h EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER) · line 244

Quoted 6 lines
fs/erofs/internal.h +3 -0
@@ -238,6 +239,10 @@ 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/maximum readahead pages of directory */
+#define DEFAULT_DIR_RA_PAGES	4
+#define MAX_DIR_RA_PAGES	128

better to add EROFS_ prefix for them.

Also could we setup those blocks or bytes instead of pages?

If users would like to setup values, they may don't care more the page size since they only care about images.

Also why do we limit maximum number even if users would like to readahead more? (such as fadvise allows -1 too)

Thanks,
Gao Xiang

author maintainer
#2 lore raw
Quoted 52 lines · Gao Xiang

Hi Chao,

Quoted 49 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.562

Meanwhile, let's introduces a new sysfs entry to control page count of readahead to provide more flexible policy for readahead of readdir().
- location: /sys/fs/erofs/<disk>/dir_ra_pages
- default value: 4
- range: [0, 128]
- disable readahead: set the value to 0

---
v2:
- introduce sysfs node to control page count of readahead during readdir().
  Documentation/ABI/testing/sysfs-fs-erofs | 7 +++++++ fs/erofs/dir.c                           | 9 +++++++++ fs/erofs/internal.h                      | 5 +++++ fs/erofs/super.c                         | 2 ++ fs/erofs/sysfs.c                         | 5 +++++

  5 files changed, 28 insertions(+)
Documentation/ABI/testing/sysfs-fs-erofs +0 -0
index bf3b6299c15e..500c93484e4c 100644
@@ -35,3 +35,10 @@ 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).

Documentation/ABI/testing/sysfs-fs-erofs +7 -0
+
+What:        /sys/fs/erofs/<disk>/dir_ra_pages
+Date:        July 2025
+Contact:    "Chao Yu" <[email protected]>
+Description:    Used to set or show page count of readahead during readdir(),
+        the range of value is [0, 128], by default it is 4, set it to
+        0 to disable readahead.
fs/erofs/dir.c +0 -0
index 3e4b38bec0aa..40f828d5b670 100644
@@ -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 -0
       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);

why using DIV_ROUND_UP_POW2 rather than DIV_ROUND_UP here?

Seems DIV_ROUND_UP_POW2() doesn't use bit shift as expected, let me use DIV_ROUND_UP() instead.

Quoted 11 lines
Quoted 8 lines
fs/erofs/dir.c +0 -0
       int err = 0;
       bool initial = true;
   @@ -63,6 +65,13 @@ 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_pages && nr_pages - dbstart > 1 &&

dbstart is a byte-oriented value, so I'm not sure if it works as you expect..

Oh, I checked patch in 6.6, found that I missed to handle it correctly when porting code to upstream, let me fix this.

Quoted 32 lines
Quoted 4 lines

+            !ra_has_index(ra, dbstart))
+            page_cache_sync_readahead(dir->i_mapping, ra, f, +                dbstart, min(nr_pages - dbstart,

same here.

Quoted 20 lines

+                (pgoff_t)EROFS_I_SB(dir)->dir_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
index 0d19bde8c094..f0e5b4273aa8 100644
@@ -157,6 +157,7 @@ struct erofs_sb_info {
       /* sysfs support */

      struct kobject s_kobj;        /* /sys/fs/erofs/<devname> */ struct completion s_kobj_unregister;
+    unsigned int dir_ra_pages;
        /* fscache support */
      struct fscache_volume *volume;

fs/erofs/internal.h +0 -0
@@ -238,6 +239,10 @@ 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/maximum readahead pages of directory */

fs/erofs/internal.h EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER) · line 244

Quoted 2 lines
fs/erofs/internal.h +2 -0
+#define DEFAULT_DIR_RA_PAGES    4
+#define MAX_DIR_RA_PAGES    128

better to add EROFS_ prefix for them.

Quoted 6 lines

Also could we setup those blocks or bytes instead of pages?

If users would like to setup values, they may don't care more the page size since they only care about images.

Let's use bytes, and then roundup to blksize?

Quoted 4 lines

Also why do we limit maximum number even if users would like to readahead more? (such as fadvise allows -1 too)

No test, but I suspect there is border effect even we set it to i_size.

Anyway, let me remove the upper boundary limitation, unless there is further requirement of limitation.

Thanks,

Quoted 4 lines

Thanks,
Gao Xiang

#3 lore raw
Quoted 62 lines · Chao Yu
Quoted 52 lines · Gao Xiang

Hi Chao,

Quoted 49 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.562

Meanwhile, let's introduces a new sysfs entry to control page count of readahead to provide more flexible policy for readahead of readdir().
- location: /sys/fs/erofs/<disk>/dir_ra_pages
- default value: 4
- range: [0, 128]
- disable readahead: set the value to 0

---
v2:
- introduce sysfs node to control page count of readahead during readdir().

   Documentation/ABI/testing/sysfs-fs-erofs | 7 +++++++
   fs/erofs/dir.c                           | 9 +++++++++
   fs/erofs/internal.h                      | 5 +++++
   fs/erofs/super.c                         | 2 ++
   fs/erofs/sysfs.c                         | 5 +++++
   5 files changed, 28 insertions(+)
Documentation/ABI/testing/sysfs-fs-erofs +7 -0
index bf3b6299c15e..500c93484e4c 100644
@@ -35,3 +35,10 @@ 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_pages
+Date:        July 2025
+Contact:    "Chao Yu" <[email protected]>
+Description:    Used to set or show page count of readahead during readdir(),
+        the range of value is [0, 128], by default it is 4, set it to
+        0 to disable readahead.
fs/erofs/dir.c +0 -0
index 3e4b38bec0aa..40f828d5b670 100644
@@ -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 -0
       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);

why using DIV_ROUND_UP_POW2 rather than DIV_ROUND_UP here?

Quoted 2 lines

Seems DIV_ROUND_UP_POW2() doesn't use bit shift as expected, let me use DIV_ROUND_UP() instead.

Oh, I think it's fine to use DIV_ROUND_UP_POW2 here.

Quoted 48 lines
Quoted 10 lines
Quoted 9 lines
fs/erofs/dir.c +0 -0
       int err = 0;
       bool initial = true;
   @@ -63,6 +65,13 @@ 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_pages && nr_pages - dbstart > 1 &&

Quoted 2 lines

dbstart is a byte-oriented value, so I'm not sure if it works as you expect..

Oh, I checked patch in 6.6, found that I missed to handle it correctly when porting code to upstream, let me fix this.

Quoted 32 lines
Quoted 4 lines

+            !ra_has_index(ra, dbstart))
+            page_cache_sync_readahead(dir->i_mapping, ra, f, +                dbstart, min(nr_pages - dbstart,

same here.

Quoted 17 lines

+                (pgoff_t)EROFS_I_SB(dir)->dir_ra_pages));

fs/erofs/dir.c +1 -0
+
           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
index 0d19bde8c094..f0e5b4273aa8 100644
@@ -157,6 +157,7 @@ struct erofs_sb_info {
       /* sysfs support */
       struct kobject s_kobj;        /* /sys/fs/erofs/<devname> */
       struct completion s_kobj_unregister;
+    unsigned int dir_ra_pages;
         /* fscache support */
       struct fscache_volume *volume;

fs/erofs/internal.h EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER) · line 243

Quoted 5 lines
fs/erofs/internal.h +2 -0
@@ -238,6 +239,10 @@ 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/maximum readahead pages of directory */
+#define DEFAULT_DIR_RA_PAGES    4
+#define MAX_DIR_RA_PAGES    128

better to add EROFS_ prefix for them.

Quoted 8 lines
Quoted 6 lines

Also could we setup those blocks or bytes instead of pages?

If users would like to setup values, they may don't care more the page size since they only care about images.

Let's use bytes, and then roundup to blksize?

Agreed.

Quoted 11 lines
Quoted 5 lines

Also why do we limit maximum number even if users would like to readahead more? (such as fadvise allows -1 too)

No test, but I suspect there is border effect even we set it to i_size.

Anyway, let me remove the upper boundary limitation, unless there is further requirement of limitation.

Ok.

Thanks,
Gao Xiang

Quoted 8 lines

Thanks,

Quoted 4 lines

Thanks,
Gao Xiang