erofs: share the superblock for file-backed mounts
Subsystems (3)
v5 cover erofs: share the superblock for file-backed mounts
This series adds a "superblock_share" mount option for file-backed EROFS mounts. When set, mounting the same backing file (identified by inode and fsoffset) with compatible options reuses the existing superblock instead of creating a new one, so the page cache and inode cache are shared across all mounts of the image. Without the option the behaviour is unchanged.
v4: https://lore.kernel.org/linux-fsdevel/[email protected]/
v3: https://lore.kernel.org/linux-fsdevel/[email protected]/
v2: https://lore.kernel.org/linux-fsdevel/[email protected]/
v1: https://lore.kernel.org/linux-fsdevel/[email protected]/
Needs: https://lore.kernel.org/linux-fsdevel/[email protected]/
Giuseppe Scrivano (2):
fs: rename vfs_get_super() to get_tree_super() and export it
erofs: reuse superblock for file-backed mounts
Documentation/filesystems/erofs.rst | 8 ++++ fs/erofs/internal.h | 1 + fs/erofs/super.c | 58 +++++++++++++++++++++++++++-- fs/super.c | 19 ++++++++-- include/linux/fs_context.h | 4 ++ 5 files changed, 83 insertions(+), 7 deletions(-)
Signature · 1 line
2.55.0
PATCH v5 1/2 fs: rename vfs_get_super() to get_tree_super() and export it
vfs_get_super() already implements the common get_tree pattern of looking up an existing superblock via a test callback and initialising a new one with fill_super otherwise, but it is private to fs/super.c and only reachable through the get_tree_nodev()/get_tree_single()/ get_tree_keyed() wrappers, none of which let a filesystem supply its own test callback.
Rename it to get_tree_super() and export it so filesystems that need a custom superblock matching policy can reuse it directly instead of open-coding sget_fc() + fill_super(). No functional change.
This is a preparatory fix for the next patch.
--- fs/super.c | 19 +++++++++++++++---- include/linux/fs_context.h | 4 ++++ 2 files changed, 19 insertions(+), 4 deletions(-)
fs/super.c +15 -4
@@ -1251,7 +1251,17 @@ static int test_single_super(struct super_block *s, struct fs_context *fc)return 1 ; } -static int vfs_get_super (struct fs_context *fc, +/** +* get_tree_super - Get a superblock, optionally sharing an existing one +* @fc: The filesystem context holding the parameters +* @test: Comparison function to find a matching existing superblock, or NULL +* @fill_super: Helper to initialise a new superblock +* +* If @test is non-NULL and matches an existing superblock, that superblock is +* reused; otherwise a new anonymous superblock is created and initialised with +* @fill_super. Passing NULL for @test always creates a new superblock. +*/ +int get_tree_super (struct fs_context *fc,int (*test)(struct super_block *,struct fs_context *),int (*fill_super)(struct super_block *sb,struct fs_context *fc))
@@ -1278,12 +1288,13 @@ static int vfs_get_super(struct fs_context *fc,deactivate_locked_super (sb);return err; } +EXPORT_SYMBOL (get_tree_super);int get_tree_nodev (struct fs_context *fc,int (*fill_super)(struct super_block *sb,struct fs_context *fc)) { -return vfs_get_super (fc,NULL , fill_super); +return get_tree_super (fc,NULL , fill_super); }EXPORT_SYMBOL (get_tree_nodev);
@@ -1291,7 +1302,7 @@ int get_tree_single(struct fs_context *fc,int (*fill_super)(struct super_block *sb, struct fs_context *fc)) { -return vfs_get_super (fc, test_single_super, fill_super); +return get_tree_super (fc, test_single_super, fill_super); }EXPORT_SYMBOL (get_tree_single);
@@ -1301,7 +1312,7 @@ int get_tree_keyed(struct fs_context *fc, void *key) { fc->s_fs_info = key; -return vfs_get_super (fc, test_keyed_super, fill_super); +return get_tree_super (fc, test_keyed_super, fill_super); }EXPORT_SYMBOL (get_tree_keyed);
include/linux/fs_context.h +4 -0
@@ -150,6 +150,10 @@ extern int vfs_parse_fs_param_source(struct fs_context *fc, struct fs_parameter *param);extern void fc_drop_locked (struct fs_context *fc); +extern int get_tree_super (struct fs_context *fc, +int (*test)(struct super_block *,struct fs_context *), +int (*fill_super)(struct super_block *sb, +struct fs_context *fc));extern int get_tree_nodev (struct fs_context *fc,int (*fill_super)(struct super_block *sb,struct fs_context *fc));
Signature · 1 line
2.55.0
Gao Xiang
maintainerRe: [PATCH v5 1/2] fs: rename vfs_get_super() to get_tree_super() and export it
(+ cc vfs maintainers)
Hi,
Quoted 7 lines · Giuseppe Scrivano
vfs_get_super()already implements the commonget_treepattern of looking up an existing superblock via a test callback and initialising a new one withfill_superotherwise, but it is private tofs/super.cand only reachable through the get_tree_nodev()/get_tree_single()/get_tree_keyed()wrappers, none of which let a filesystem supply its own test callback.
Quoted 7 lines
Rename it to
get_tree_super()and export it so filesystems that need a custom superblock matching policy can reuse it directly instead of open-codingsget_fc()+fill_super(). No functional change.This is a preparatory fix for the next patch.
Signed-off-by Giuseppe Scrivano <[email protected]>
With the new vfs_get_super() helper, it seems much cleaner compared to the previous versions.
Since this series interacts with the other erofs ongoing patches. So I hope at least [PATCH 2/2] can be routed into the erofs tree to avoid unnecessary conflict resolving.
Maybe the simplistic way is vfs maintainers can ack this vfs patch so that both patches can go through erofs tree for the next cycle directly.
Quoted 1 line
---
...
Quoted 15 lines
fs/super.c +11
-0+/** +* get_tree_super - Get a superblock, optionally sharing an existing one +* @fc: The filesystem context holding the parameters +* @test: Comparison function to find a matching existing superblock, or NULL +* @fill_super: Helper to initialise a new superblock +* +* If @test is non-NULL and matches an existing superblock, that superblock is +* reused; otherwise a new anonymous superblock is created and initialised with +* @fill_super. Passing NULL for @test always creates a new superblock. +*/ +int get_tree_super (struct fs_context *fc,int (*test)(struct super_block *,struct fs_context *),int (*fill_super)(struct super_block *sb,struct fs_context *fc))
fs/super.c static int vfs_get_super(struct fs_context *fc, · line 1291
Quoted 4 lines
fs/super.c +1
-0@@ -1278,12 +1288,13 @@ static int vfs_get_super(struct fs_context *fc,deactivate_locked_super (sb);return err; } +EXPORT_SYMBOL (get_tree_super);
btw, some people prefer EXPORT_SYMBOL_GPL() for this kind of helpers.
Thanks,
Gao Xiang
Re: [PATCH v5 1/2] fs: rename vfs_get_super() to get_tree_super() and export it
Quoted 49 lines · Gao Xiang
(+ cc vfs maintainers)
Hi,
Quoted 7 lines · Giuseppe Scrivano
vfs_get_super()already implements the commonget_treepattern of looking up an existing superblock via a test callback and initialising a new one withfill_superotherwise, but it is private tofs/super.cand only reachable through the get_tree_nodev()/get_tree_single()/get_tree_keyed()wrappers, none of which let a filesystem supply its own test callback.Quoted 7 lines
Rename it to
get_tree_super()and export it so filesystems that need a custom superblock matching policy can reuse it directly instead of open-codingsget_fc()+fill_super(). No functional change.This is a preparatory fix for the next patch.
Signed-off-by Giuseppe Scrivano <[email protected]>With the new
vfs_get_super()helper, it seems much cleaner compared to the previous versions.Since this series interacts with the other erofs ongoing patches. So I hope at least [PATCH 2/2] can be routed into the erofs tree to avoid unnecessary conflict resolving.
Maybe the simplistic way is vfs maintainers can ack this vfs patch so that both patches can go through erofs tree for the next cycle directly.
Quoted 1 line
---
...
Quoted 13 lines
fs/super.c +11
-0+/** +* get_tree_super - Get a superblock, optionally sharing an existing one +* @fc: The filesystem context holding the parameters +* @test: Comparison function to find a matching existing superblock, or NULL +* @fill_super: Helper to initialise a new superblock +* +* If @test is non-NULL and matches an existing superblock, that superblock is +* reused; otherwise a new anonymous superblock is created and initialised with +* @fill_super. Passing NULL for @test always creates a new superblock. +*/ +int get_tree_super(struct fs_context *fc, int (*test)(struct super_block *, struct fs_context *), int (*fill_super)(struct super_block *sb,
Quoted 8 lines
fs/super.c static int vfs_get_super(struct fs_context *fc, · line 1291
Quoted 6 lines
fs/super.c +1
-0struct fs_context *fc))@@ -1278,12 +1288,13 @@ static int vfs_get_super(struct fs_context *fc,deactivate_locked_super (sb);return err; } +EXPORT_SYMBOL (get_tree_super);btw, some people prefer
EXPORT_SYMBOL_GPL()for this kind of helpers.
I am fine with this change, but the rest of the file is using EXPORT_SYMBOL though, that is why I've used it.
Regards,
Giuseppe
Christian Brauner
maintainerRe: [PATCH v5 1/2] fs: rename vfs_get_super() to get_tree_super() and export it
In reply to Gao XiangQuoted 16 lines · Gao Xiang
(+ cc vfs maintainers)
Hi,
Quoted 11 lines · Giuseppe Scrivano
vfs_get_super()already implements the commonget_treepattern of looking up an existing superblock via a test callback and initialising a new one withfill_superotherwise, but it is private tofs/super.cand only reachable through the get_tree_nodev()/get_tree_single()/get_tree_keyed()wrappers, none of which let a filesystem supply its own test callback.Rename it to
get_tree_super()and export it so filesystems that need a custom superblock matching policy can reuse it directly instead of open-codingsget_fc()+fill_super(). No functional change.
Quoted 6 lines
Quoted 3 lines
This is a preparatory fix for the next patch.
Signed-off-by Giuseppe Scrivano <[email protected]>With the new
vfs_get_super()helper, it seems much cleaner compared to the previous versions.
I'll add this patch to the vfs-7.3.super tree post -rc1 and then you can pull it in. Thanks!
Gao Xiang
maintainerRe: [PATCH v5 1/2] fs: rename vfs_get_super() to get_tree_super() and export it
Hi Christian,
Quoted 19 lines · Christian Brauner
Quoted 18 lines · Gao Xiang
(+ cc vfs maintainers)
Hi,
Quoted 13 lines · Giuseppe Scrivano
vfs_get_super()already implements the commonget_treepattern of looking up an existing superblock via a test callback and initialising a new one withfill_superotherwise, but it is private tofs/super.cand only reachable through the get_tree_nodev()/get_tree_single()/get_tree_keyed()wrappers, none of which let a filesystem supply its own test callback.Rename it to
get_tree_super()and export it so filesystems that need a custom superblock matching policy can reuse it directly instead of open-codingsget_fc()+fill_super(). No functional change.This is a preparatory fix for the next patch.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Giuseppe Scrivano <[email protected]>With the new
vfs_get_super()helper, it seems much cleaner compared to the previous versions.I'll add this patch to the vfs-7.3.super tree post -rc1 and then you can pull it in. Thanks!
I guess you meant vfs-7.4.super? If yes that would be fine, I'm willing to land this feature for 7.4 too.
Thanks,
Gao Xiang
Quoted 1 line
Gao Xiang
maintainerRe: [PATCH v5 1/2] fs: rename vfs_get_super() to get_tree_super() and export it
In reply to Christian BraunerHi Christian,
Quoted 19 lines · Christian Brauner
Quoted 18 lines · Gao Xiang
(+ cc vfs maintainers)
Hi,
Quoted 13 lines · Giuseppe Scrivano
vfs_get_super()already implements the commonget_treepattern of looking up an existing superblock via a test callback and initialising a new one withfill_superotherwise, but it is private tofs/super.cand only reachable through the get_tree_nodev()/get_tree_single()/get_tree_keyed()wrappers, none of which let a filesystem supply its own test callback.Rename it to
get_tree_super()and export it so filesystems that need a custom superblock matching policy can reuse it directly instead of open-codingsget_fc()+fill_super(). No functional change.This is a preparatory fix for the next patch.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Giuseppe Scrivano <[email protected]>With the new
vfs_get_super()helper, it seems much cleaner compared to the previous versions.I'll add this patch to the vfs-7.3.super tree post -rc1 and then you can pull it in. Thanks!
friendly ping.. since I'm working on 7.4 cycle too, could you add the latest v6 series to vfs-7.4.super so I could pull it in too?
https://lore.kernel.org/r/[email protected]
Thanks,
Gao Xiang
Quoted 1 line
PATCH v5 2/2 erofs: reuse superblock for file-backed mounts
In reply to Giuseppe ScrivanoWhen the same file-backed image is mounted multiple times (via path or fd) with the superblock_share mount option, reuse the existing superblock instead of creating a new one. This allows multiple mounts of the same image to share in-kernel data structures more efficiently.
The backing file is identified by its inode and fsoffset. If mount options conflict or extra devices are used, a separate superblock is created transparently as a fallback. Remount is not allowed on shared superblocks.
Tested by mounting a 177M Fedora EROFS image 20 times with full traversal:
#!/bin/sh
IMG=${1:-/root/fedora.erofs}
OPTS=${2:+-o $2}
N=20
DIR=$(mktemp -d)
trap "umount $DIR/m* 2>/dev/null; rm -rf $DIR" EXIT
sync; echo 3 > /proc/sys/vm/drop_caches
INODES_BEFORE=$(grep erofs_inode /proc/slabinfo | awk '{print $2}')
MEM_BEFORE=$(grep ^Slab: /proc/meminfo | awk '{print $2}')
for i in $(seq 1 $N); do
mkdir $DIR/m$i
mount -t erofs $OPTS "$IMG" $DIR/m$i
find $DIR/m$i > /dev/null
done
echo "Superblocks: $(grep $DIR /proc/self/mountinfo | \
awk '{print $3}' | sort -u | wc -l)"
echo "erofs_inode delta: +$(( $(grep erofs_inode /proc/slabinfo | \
awk '{print $2}') - INODES_BEFORE ))"
echo "Slab delta: +$(( $(grep ^Slab: /proc/meminfo | \
awk '{print $2}') - MEM_BEFORE )) kB"without superblock_share:
# time ./test.sh /root/fedora.erofs Superblocks: 20 erofs_inode delta: +45864 Slab delta: +37448 kB real 0m2.311s user 0m0.222s sys 0m1.998s
with superblock_share:
# time ./test.sh /root/fedora.erofs superblock_share Superblocks: 1 erofs_inode delta: +2044 Slab delta: +284 kB real 0m0.679s user 0m0.157s sys 0m0.469s
The time difference shows that sharing the superblock also benefits the page cache and inode cache, as subsequent mounts of the same image avoid re-reading the backing file. This is particularly useful for container hosts running multiple containers from the same base image.
--- Documentation/filesystems/erofs.rst | 8 ++++ fs/erofs/internal.h | 1 + fs/erofs/super.c | 58 +++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-)
Documentation/filesystems/erofs.rst +8 -0
@@ -139,6 +139,9 @@ inode_share Enable inode page sharing for this filesystem. Inodes wi page cache. source=%s (For file-backed mounts) Specify the backing image as a path or as an already-opened file descriptor. +superblock_share (For file-backed mounts) Share the superblock when the same + backing file is mounted more than once with compatible + options. Remount is not allowed on shared superblocks. =================== ========================================================= File-backed mounts
@@ -156,6 +159,11 @@ Only regular files are accepted as backing files; to mount an image that resides on a block device, use the traditional block device mount path instead. +When superblock_share is specified and the same backing file (identified +by inode and fsoffset) is mounted more than once with compatible mount +options, the kernel reuses the existing superblock instead of creating a +new one. Remount is not allowed on shared superblocks. + Sysfs Entries =============
fs/erofs/internal.h +1 -0
@@ -156,6 +156,7 @@ struct erofs_sb_info {#define EROFS_MOUNT_DAX_NEVER 0x00000080#define EROFS_MOUNT_DIRECT_IO 0x00000100#define EROFS_MOUNT_INODE_SHARE 0x00000200 +#define EROFS_MOUNT_SUPERBLOCK_SHARE 0x00000400#define clear_opt (opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)#define set_opt (opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)
fs/erofs/super.c +55 -3
106 lines not on this page · raw message
Signature · 1 line
2.55.0
Gao Xiang
maintainerRe: [PATCH v5 2/2] erofs: reuse superblock for file-backed mounts
Hi Giuseppe,
Quoted 57 lines · Giuseppe Scrivano
When the same file-backed image is mounted multiple times (via path or fd) with the
superblock_sharemount option, reuse the existing superblock instead of creating a new one. This allows multiple mounts of the same image to share in-kernel data structures more efficiently.The backing file is identified by its inode and fsoffset. If mount options conflict or extra devices are used, a separate superblock is created transparently as a fallback. Remount is not allowed on shared superblocks.
Tested by mounting a 177M Fedora EROFS image 20 times with full traversal:
#!/bin/sh IMG=${1:-/root/fedora.erofs} OPTS=${2:+-o $2} N=20 DIR=$(mktemp -d) trap "umount $DIR/m* 2>/dev/null; rm -rf $DIR" EXIT sync; echo 3 > /proc/sys/vm/drop_caches INODES_BEFORE=$(grep erofs_inode /proc/slabinfo | awk '{print $2}') MEM_BEFORE=$(grep ^Slab: /proc/meminfo | awk '{print $2}') for i in $(seq 1 $N); do mkdir $DIR/m$i mount -t erofs $OPTS "$IMG" $DIR/m$i find $DIR/m$i > /dev/null done echo "Superblocks: $(grep $DIR /proc/self/mountinfo | \ awk '{print $3}' | sort -u | wc -l)" echo "erofs_inode delta: +$(( $(grep erofs_inode /proc/slabinfo | \ awk '{print $2}') - INODES_BEFORE ))" echo "Slab delta: +$(( $(grep ^Slab: /proc/meminfo | \ awk '{print $2}') - MEM_BEFORE )) kB"without
superblock_share:# time ./test.sh /root/fedora.erofs Superblocks: 20 erofs_inode delta: +45864 Slab delta: +37448 kB real 0m2.311s user 0m0.222s sys 0m1.998swith
superblock_share:# time ./test.sh /root/fedora.erofs superblock_share Superblocks: 1 erofs_inode delta: +2044 Slab delta: +284 kB real 0m0.679s user 0m0.157s sys 0m0.469s
Quoted 6 lines
The time difference shows that sharing the superblock also benefits the page cache and inode cache, as subsequent mounts of the same image avoid re-reading the backing file. This is particularly useful for container hosts running multiple containers from the same base image.
Signed-off-by Giuseppe Scrivano <[email protected]>
Thanks for the patch.
It generally looks good to me. Just some minor nits:
Quoted 21 lines
--- Documentation/filesystems/erofs.rst | 8 ++++ fs/erofs/internal.h | 1 + fs/erofs/super.c | 58 +++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-)Documentation/filesystems/erofs.rst +3
-0@@ -139,6 +139,9 @@ inode_share Enable inode page sharing for this filesystem. Inodes wi page cache. source=%s (For file-backed mounts) Specify the backing image as a path or as an already-opened file descriptor. +superblock_share (For file-backed mounts) Share the superblock when the same + backing file is mounted more than once with compatible + options. Remount is not allowed on shared superblocks. =================== ========================================================= File-backed mounts
Documentation/filesystems/erofs.rst Only regular files are accepted as backing files; to mount an image that · line 164
Quoted 6 lines
Documentation/filesystems/erofs.rst +3
-0@@ -156,6 +159,11 @@ Only regular files are accepted as backing files; to mount an image that resides on a block device, use the traditional block device mount path instead. +When superblock_share is specified and the same backing file (identified +by inode and fsoffset) is mounted more than once with compatible mount +options, the kernel reuses the existing superblock instead of creating a
When superblock_share is specified, multiple mounts of the same backing file at the same fsoffset with compatible mount options can share a single superblock. Remount is now allowed on shared superblocks.
Quoted 52 lines
Documentation/filesystems/erofs.rst +2
-0+new one. Remount is not allowed on shared superblocks. + Sysfs Entries =============fs/erofs/internal.h +1
-0@@ -156,6 +156,7 @@ struct erofs_sb_info {#define EROFS_MOUNT_DAX_NEVER 0x00000080#define EROFS_MOUNT_DIRECT_IO 0x00000100#define EROFS_MOUNT_INODE_SHARE 0x00000200 +#define EROFS_MOUNT_SUPERBLOCK_SHARE 0x00000400#define clear_opt (opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)#define set_opt (opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)fs/erofs/super.c +8
-1@@ -386,7 +386,7 @@ static void erofs_default_options(struct erofs_sb_info *sbi)enum { Opt_user_xattr, Opt_acl, Opt_cache_strategy, Opt_dax, Opt_dax_enum, Opt_device, Opt_domain_id, Opt_directio, Opt_fsoffset, Opt_inode_share, - Opt_source, + Opt_source, Opt_superblock_share, };static const struct constant_table erofs_param_cache_strategy[] = {@@ -415,6 +415,7 @@ static const struct fs_parameter_spec erofs_fs_parameters[] = {fsparam_u64 ("fsoffset" , Opt_fsoffset),fsparam_flag ("inode_share" , Opt_inode_share),fsparam_file_or_string ("source" , Opt_source), + fsparam_flag("superblock_share", Opt_superblock_share), {} };@@ -560,6 +561,12 @@ static int erofs_fc_parse_param(struct fs_context *fc,else set_opt (&sbi->opt,INODE_SHARE );break ; +case Opt_superblock_share: +if (!IS_ENABLED (CONFIG_EROFS_FS_BACKED_BY_FILE )) +errorfc (fc,"%s option not supported" , erofs_fs_parameters[opt].name); +else +set_opt (&sbi->opt,SUPERBLOCK_SHARE ); +break ;case Opt_source:return erofs_fc_parse_source (fc, param); }
fs/erofs/super.c static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) · line 678
Quoted 9 lines
fs/erofs/super.c +6
-0@@ -663,6 +670,17 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)errorfc (fc,"FSDAX is not allowed when inode_share is on" );return -EINVAL ; } +if (test_opt (&sbi->opt,SUPERBLOCK_SHARE ) && +test_opt (&sbi->opt,INODE_SHARE )) { +errorfc (fc,"superblock_share is not allowed when inode_share is on" ); +return -EINVAL ; + } +/* Extra devices are not yet supported with superblock sharing. */
Okay, if the comment is added here, I don't think it's helpful since the error message indicates the same.
fs/erofs/super.c static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) · line 808
Quoted 12 lines
fs/erofs/super.c +5
-0+if (test_opt (&sbi->opt,SUPERBLOCK_SHARE ) && + sbi->devs->extra_devices) { +errorfc (fc,"superblock_share does not support extra devices" ); +return -EINVAL ; + } sbi->blkszbits =PAGE_SHIFT ;if (!sb->s_bdev) {@@ -788,6 +806,36 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)return 0 ; }
Maybe just move the comment below here:
/*
* The function is used only with a sbi that has SUPERBLOCK_SHARE set,
* so that the memcmp later ensures new_sbi is also shareable. */
fs/erofs/super.c static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) · line 809
Quoted 1 line
fs/erofs/super.c +1
-0+static int erofs_fc_test_file_super(struct super_block *sb,
Can we just rename it as erofs_sb_share_test_super()?
Quoted 10 lines
fs/erofs/super.c +10
-0+ struct fs_context *fc) +{ +/* +* The assumption is that the function is used only with a sbi that +* has SUPERBLOCK_SHARE set, so that the memcmp later ensures +* new_sbi also is shareable. +*/ +struct erofs_sb_info *sbi =EROFS_SB (sb); +struct erofs_sb_info *new_sbi = fc->s_fs_info; +
fs/erofs/super.c static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) · line 829
Quoted 10 lines
fs/erofs/super.c +10
-0+if (sb->s_iflags &SB_I_RETIRED ) +return 0 ; +if (!sbi->dif0.file || !new_sbi->dif0.file) +return 0 ; +return file_inode (sbi->dif0.file) ==file_inode (new_sbi->dif0.file) && + sbi->dif0.fsoff == new_sbi->dif0.fsoff && + !memcmp (&sbi->opt, &new_sbi->opt,sizeof (sbi->opt)); +} + +static int erofs_fc_get_tree_file (struct fs_context *fc)
Could we just rename it as erofs_file_get_tree()?
Thanks,
Gao Xiang
Re: [PATCH v5 2/2] erofs: reuse superblock for file-backed mounts
Quoted 206 lines · Gao Xiang
Hi Giuseppe,
Quoted 57 lines · Giuseppe Scrivano
When the same file-backed image is mounted multiple times (via path or fd) with the
superblock_sharemount option, reuse the existing superblock instead of creating a new one. This allows multiple mounts of the same image to share in-kernel data structures more efficiently.The backing file is identified by its inode and fsoffset. If mount options conflict or extra devices are used, a separate superblock is created transparently as a fallback. Remount is not allowed on shared superblocks.
Tested by mounting a 177M Fedora EROFS image 20 times with full traversal:
#!/bin/sh IMG=${1:-/root/fedora.erofs} OPTS=${2:+-o $2} N=20 DIR=$(mktemp -d) trap "umount $DIR/m* 2>/dev/null; rm -rf $DIR" EXIT sync; echo 3 > /proc/sys/vm/drop_caches INODES_BEFORE=$(grep erofs_inode /proc/slabinfo | awk '{print $2}') MEM_BEFORE=$(grep ^Slab: /proc/meminfo | awk '{print $2}') for i in $(seq 1 $N); do mkdir $DIR/m$i mount -t erofs $OPTS "$IMG" $DIR/m$i find $DIR/m$i > /dev/null done echo "Superblocks: $(grep $DIR /proc/self/mountinfo | \ awk '{print $3}' | sort -u | wc -l)" echo "erofs_inode delta: +$(( $(grep erofs_inode /proc/slabinfo | \ awk '{print $2}') - INODES_BEFORE ))" echo "Slab delta: +$(( $(grep ^Slab: /proc/meminfo | \ awk '{print $2}') - MEM_BEFORE )) kB"without
superblock_share:# time ./test.sh /root/fedora.erofs Superblocks: 20 erofs_inode delta: +45864 Slab delta: +37448 kB real 0m2.311s user 0m0.222s sys 0m1.998swith
superblock_share:# time ./test.sh /root/fedora.erofs superblock_share Superblocks: 1 erofs_inode delta: +2044 Slab delta: +284 kB real 0m0.679s user 0m0.157s sys 0m0.469sQuoted 6 lines
The time difference shows that sharing the superblock also benefits the page cache and inode cache, as subsequent mounts of the same image avoid re-reading the backing file. This is particularly useful for container hosts running multiple containers from the same base image.
Signed-off-by Giuseppe Scrivano <[email protected]>Thanks for the patch.
It generally looks good to me. Just some minor nits:
Quoted 21 lines
--- Documentation/filesystems/erofs.rst | 8 ++++ fs/erofs/internal.h | 1 + fs/erofs/super.c | 58 +++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-)Documentation/filesystems/erofs.rst +3
-0@@ -139,6 +139,9 @@ inode_share Enable inode page sharing for this filesystem. Inodes wi page cache. source=%s (For file-backed mounts) Specify the backing image as a path or as an already-opened file descriptor. +superblock_share (For file-backed mounts) Share the superblock when the same + backing file is mounted more than once with compatible + options. Remount is not allowed on shared superblocks. =================== ========================================================= File-backed mountsDocumentation/filesystems/erofs.rst Only regular files are accepted as backing files; to mount an image that · line 164
Quoted 6 lines
Documentation/filesystems/erofs.rst +3
-0@@ -156,6 +159,11 @@ Only regular files are accepted as backing files; to mount an image that resides on a block device, use the traditional block device mount path instead. +When superblock_share is specified and the same backing file (identified +by inode and fsoffset) is mounted more than once with compatible mount +options, the kernel reuses the existing superblock instead of creating aWhen
superblock_shareis specified, multiple mounts of the same backing file at the same fsoffset with compatible mount options can share a single superblock. Remount is now allowed on shared superblocks.Quoted 52 lines
Documentation/filesystems/erofs.rst +2
-0+new one. Remount is not allowed on shared superblocks. + Sysfs Entries =============fs/erofs/internal.h +1
-0@@ -156,6 +156,7 @@ struct erofs_sb_info {#define EROFS_MOUNT_DAX_NEVER 0x00000080#define EROFS_MOUNT_DIRECT_IO 0x00000100#define EROFS_MOUNT_INODE_SHARE 0x00000200 +#define EROFS_MOUNT_SUPERBLOCK_SHARE 0x00000400#define clear_opt (opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)#define set_opt (opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)fs/erofs/super.c +8
-1@@ -386,7 +386,7 @@ static void erofs_default_options(struct erofs_sb_info *sbi)enum { Opt_user_xattr, Opt_acl, Opt_cache_strategy, Opt_dax, Opt_dax_enum, Opt_device, Opt_domain_id, Opt_directio, Opt_fsoffset, Opt_inode_share, - Opt_source, + Opt_source, Opt_superblock_share, };static const struct constant_table erofs_param_cache_strategy[] = {@@ -415,6 +415,7 @@ static const struct fs_parameter_spec erofs_fs_parameters[] = {fsparam_u64 ("fsoffset" , Opt_fsoffset),fsparam_flag ("inode_share" , Opt_inode_share),fsparam_file_or_string ("source" , Opt_source), + fsparam_flag("superblock_share", Opt_superblock_share), {} };@@ -560,6 +561,12 @@ static int erofs_fc_parse_param(struct fs_context *fc,else set_opt (&sbi->opt,INODE_SHARE );break ; +case Opt_superblock_share: +if (!IS_ENABLED (CONFIG_EROFS_FS_BACKED_BY_FILE )) +errorfc (fc,"%s option not supported" , erofs_fs_parameters[opt].name); +else +set_opt (&sbi->opt,SUPERBLOCK_SHARE ); +break ;case Opt_source:return erofs_fc_parse_source (fc, param); }fs/erofs/super.c static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) · line 678
Quoted 9 lines
fs/erofs/super.c +6
-0@@ -663,6 +670,17 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)errorfc (fc,"FSDAX is not allowed when inode_share is on" );return -EINVAL ; } +if (test_opt (&sbi->opt,SUPERBLOCK_SHARE ) && +test_opt (&sbi->opt,INODE_SHARE )) { +errorfc (fc,"superblock_share is not allowed when inode_share is on" ); +return -EINVAL ; + } +/* Extra devices are not yet supported with superblock sharing. */ Okay, if the comment is added here, I don't think it's helpful since the error message indicates the same.
fs/erofs/super.c static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) · line 808
Quoted 12 lines
fs/erofs/super.c +5
-0+if (test_opt (&sbi->opt,SUPERBLOCK_SHARE ) && + sbi->devs->extra_devices) { +errorfc (fc,"superblock_share does not support extra devices" ); +return -EINVAL ; + } sbi->blkszbits =PAGE_SHIFT ;if (!sb->s_bdev) {@@ -788,6 +806,36 @@ static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)return 0 ; }Maybe just move the comment below here:
/*
* The function is used only with a sbi that hasSUPERBLOCK_SHAREset,
* so that the memcmp later ensuresnew_sbiis also shareable. */fs/erofs/super.c static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) · line 809
Quoted 1 line
fs/erofs/super.c +1
-0+static int erofs_fc_test_file_super(struct super_block *sb,Can we just rename it as
erofs_sb_share_test_super()?Quoted 14 lines
fs/erofs/super.c +14
-0+ struct fs_context *fc) +{ +/* +* The assumption is that the function is used only with a sbi that +* has SUPERBLOCK_SHARE set, so that the memcmp later ensures +* new_sbi also is shareable. +*/ +struct erofs_sb_info *sbi =EROFS_SB (sb); +struct erofs_sb_info *new_sbi = fc->s_fs_info; + +if (sb->s_iflags &SB_I_RETIRED ) +return 0 ; +if (!sbi->dif0.file || !new_sbi->dif0.file) +return 0 ;
Quoted 8 lines
fs/erofs/super.c static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) · line 829
Quoted 6 lines
fs/erofs/super.c +6
-0+return file_inode (sbi->dif0.file) ==file_inode (new_sbi->dif0.file) && + sbi->dif0.fsoff == new_sbi->dif0.fsoff && + !memcmp (&sbi->opt, &new_sbi->opt,sizeof (sbi->opt)); +} + +static int erofs_fc_get_tree_file (struct fs_context *fc)Could we just rename it as
erofs_file_get_tree()?
sure, thanks for the review. I'll send another version later today/tomorrow if there are no other comments.
Regards,
Giuseppe