Squashfs: add page cache share support
PATCH 1/1 Squashfs: add page cache share support
Last year, Hongzhen Luo submitted the page cache sharing feature for the EROFS file system, highlighting that in container scenarios, different images mounted at separate mount points cause identical files to maintain duplicate page caches. This issue also exists in the SquashFS file system, so we added support for page cache sharing on SquashFS.
To enable page cache sharing, the extended attribute of each file must be configured. This involves calculating the file's MD5 value and setting it via the setfattr command.
# md5sum $file
# setfattr -n "trusted.md5sum" -v "$hash" $fileA 300MB file was packaged into two separate images, which were mounted at different mount points. fio was used to read the file contents, comparing the system page cache size with and without the page cache sharing feature enabled.
|---------------|-------------------------|--------------------------| | | enable page cache share | disable page cache share | |---------------|-------------------------|--------------------------| |page cache size| 501MiB | 1052MiB | |---------------|-------------------------|--------------------------|
--- fs/squashfs/Kconfig | 10 +++ fs/squashfs/Makefile | 1 + fs/squashfs/inode.c | 10 ++- fs/squashfs/pagecache_share.c | 159 ++++++++++++++++++++++++++++++++++ fs/squashfs/pagecache_share.h | 22 +++++ fs/squashfs/squashfs.h | 2 + fs/squashfs/squashfs_fs_i.h | 5 ++ fs/squashfs/super.c | 45 +++++++++- 8 files changed, 252 insertions(+), 2 deletions(-) create mode 100644 fs/squashfs/pagecache_share.c create mode 100644 fs/squashfs/pagecache_share.h
fs/squashfs/Kconfig +10 -0
@@ -285,3 +285,13 @@ config SQUASHFS_FRAGMENT_CACHE_SIZE Note there must be at least one cached fragment. Anything much more than three will probably not make much difference. + +config SQUASHFS_PAGE_CACHE_SHARE + bool "SQUASHFS page cache share support" + depends on SQUASHFS + default n + help + Saying Y here includes support for permiting SQUASHFS to share + page cache for files with same fingerprints. + + If unsure, say N.
fs/squashfs/Makefile +1 -0
@@ -17,3 +17,4 @@ squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o squashfs-$(CONFIG_SQUASHFS_XZ) += xz_wrapper.o squashfs-$(CONFIG_SQUASHFS_ZLIB) += zlib_wrapper.o squashfs-$(CONFIG_SQUASHFS_ZSTD) += zstd_wrapper.o +squashfs-$(CONFIG_SQUASHFS_PAGE_CACHE_SHARE) += pagecache_share.o
fs/squashfs/inode.c +9 -1
@@ -35,6 +35,7 @@#include "squashfs_fs_i.h" #include "squashfs.h" #include "xattr.h" +#include "pagecache_share.h" /* * Initialise VFS inode with the base inode information common to all
@@ -90,8 +91,15 @@ struct inode *squashfs_iget(struct super_block *sb, long long ino,iget_failed (inode);return ERR_PTR (err); } -unlock_new_inode (inode); + +#ifdef CONFIG_SQUASHFS_PAGE_CACHE_SHARE +if ((inode->i_mode &S_IFMT ) ==S_IFREG ) { +if (squashfs_pcs_fill_inode (inode) >0 ) + inode->i_fop = &squashfs_pcs_file_fops; + } +#endif +return inode; }
fs/squashfs/pagecache_share.c new file +159 -0
159 lines not on this page · raw message
fs/squashfs/pagecache_share.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2024, Inspur +*/ +#ifndef __SQUASHFS_PAGECACHE_SHARE_H +#define __SQUASHFS_PAGECACHE_SHARE_H + +#include <linux/mutex.h> +#include <linux/fs.h> +#include <linux/mount.h> +#include <linux/rwlock.h> +#include <linux/mutex.h> + +int squashfs_pcs_fill_inode (struct inode *inode); +int sqyashfs_pcs_remove (struct inode *inode); +int squashfs_pcs_init_mnt (void ); +void squashfs_pcs_mnt_exit (void ); + +extern const struct file_operations squashfs_pcs_file_fops; +extern const struct vm_operations_struct generic_file_vm_ops; +#endif +
fs/squashfs/squashfs.h +2 -0
@@ -117,3 +117,5 @@ extern const struct inode_operations squashfs_symlink_inode_ops;/* xattr.c */ extern const struct xattr_handler *const squashfs_xattr_handlers[]; + +extern struct file_system_type squashfs_anon_fs_type;
fs/squashfs/squashfs_fs_i.h +5 -0
@@ -30,6 +30,11 @@ struct squashfs_inode_info {int parent; }; }; + +#ifdef CONFIG_SQUASHFS_PAGE_CACHE_SHARE +unsigned long fprt_hash; +struct inode *pcs_inode; +#endif struct inode vfs_inode; };
fs/squashfs/super.c +44 -1
81 lines not on this page · raw message
Signature · 1 line
2.31.1
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Bo,
kernel test robot noticed the following build warnings:
[auto build test WARNING on v6.16-rc3]
[also build test WARNING on linus/master next-20250626]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Bo-Liu/Squashfs-add-page-cache-share-support/20250626-084010 base: v6.16-rc3 patch link: https://lore.kernel.org/r/20250626003644.3675-1-liubo03%40inspur.com patch subject: [PATCH] Squashfs: add page cache share support config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20250627/[email protected]/config) compiler: loongarch64-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250627/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from fs/squashfs/pagecache_share.c:18:
>> fs/squashfs/squashfs.h:25:40: warning: 'struct squashfs_page_actor' declared inside parameter list will not be visible outside of this definition or declaration
25 | struct squashfs_page_actor *);
| ^~~~~~~~~~~~~~~~~~~
>> fs/squashfs/squashfs.h:49:33: warning: 'struct squashfs_sb_info' declared inside parameter list will not be visible outside of this definition or declaration
49 | void * (*create)(struct squashfs_sb_info *msblk, void *comp_opts);
| ^~~~~~~~~~~~~~~~
fs/squashfs/squashfs.h:50:32: warning: 'struct squashfs_sb_info' declared inside parameter list will not be visible outside of this definition or declaration
50 | void (*destroy)(struct squashfs_sb_info *msblk);
| ^~~~~~~~~~~~~~~~
fs/squashfs/squashfs.h:52:58: warning: 'struct squashfs_page_actor' declared inside parameter list will not be visible outside of this definition or declaration
52 | int offset, int length, struct squashfs_page_actor *output);
| ^~~~~~~~~~~~~~~~~~~ fs/squashfs/squashfs.h:51:34: warning: 'struct squashfs_sb_info' declared inside parameter list will not be visible outside of this definition or declaration
51 | int (*decompress)(struct squashfs_sb_info *msblk, struct bio *bio,
| ^~~~~~~~~~~~~~~~
vim +25 fs/squashfs/squashfs.h
f5cc08737507f2 Phillip Lougher 2024-12-29 22
ffae2cd73a9e82 Phillip Lougher 2009-01-05 23 /* block.c */
846b730e99518a Phillip Lougher 2013-11-18 24 extern int squashfs_read_data(struct super_block *, u64, int, u64 *,
846b730e99518a Phillip Lougher 2013-11-18 @25 struct squashfs_page_actor *);
ffae2cd73a9e82 Phillip Lougher 2009-01-05 26
ffae2cd73a9e82 Phillip Lougher 2009-01-05 27 /* cache.c */
ffae2cd73a9e82 Phillip Lougher 2009-01-05 28 extern struct squashfs_cache *squashfs_cache_init(char *, int, int);
ffae2cd73a9e82 Phillip Lougher 2009-01-05 29 extern void squashfs_cache_delete(struct squashfs_cache *);
ffae2cd73a9e82 Phillip Lougher 2009-01-05 30 extern struct squashfs_cache_entry *squashfs_cache_get(struct super_block *,
ffae2cd73a9e82 Phillip Lougher 2009-01-05 31 struct squashfs_cache *, u64, int);
ffae2cd73a9e82 Phillip Lougher 2009-01-05 32 extern void squashfs_cache_put(struct squashfs_cache_entry *);
ffae2cd73a9e82 Phillip Lougher 2009-01-05 33 extern int squashfs_copy_data(void *, struct squashfs_cache_entry *, int, int);
ffae2cd73a9e82 Phillip Lougher 2009-01-05 34 extern int squashfs_read_metadata(struct super_block *, void *, u64 *,
ffae2cd73a9e82 Phillip Lougher 2009-01-05 35 int *, int);
ffae2cd73a9e82 Phillip Lougher 2009-01-05 36 extern struct squashfs_cache_entry *squashfs_get_fragment(struct super_block *,
ffae2cd73a9e82 Phillip Lougher 2009-01-05 37 u64, int);
ffae2cd73a9e82 Phillip Lougher 2009-01-05 38 extern struct squashfs_cache_entry *squashfs_get_datablock(struct super_block *,
ffae2cd73a9e82 Phillip Lougher 2009-01-05 39 u64, int);
82de647e1f81fd Phillip Lougher 2011-05-20 40 extern void *squashfs_read_table(struct super_block *, u64, int);
ffae2cd73a9e82 Phillip Lougher 2009-01-05 41
4c0f0bb2351bee Phillip Lougher 2009-10-06 42 /* decompressor.c */
4c0f0bb2351bee Phillip Lougher 2009-10-06 43 extern const struct squashfs_decompressor *squashfs_lookup_decompressor(int);
9508c6b90b3f57 Phillip Lougher 2013-11-13 44 extern void *squashfs_decompressor_setup(struct super_block *, unsigned short);
9508c6b90b3f57 Phillip Lougher 2013-11-13 45
9508c6b90b3f57 Phillip Lougher 2013-11-13 46 /* decompressor_xxx.c */
80f784098ff44e Xiaoming Ni 2022-10-19 47
80f784098ff44e Xiaoming Ni 2022-10-19 48 struct squashfs_decompressor_thread_ops {
80f784098ff44e Xiaoming Ni 2022-10-19 @49 void * (*create)(struct squashfs_sb_info *msblk, void *comp_opts);
80f784098ff44e Xiaoming Ni 2022-10-19 50 void (*destroy)(struct squashfs_sb_info *msblk);
80f784098ff44e Xiaoming Ni 2022-10-19 51 int (*decompress)(struct squashfs_sb_info *msblk, struct bio *bio,
80f784098ff44e Xiaoming Ni 2022-10-19 52 int offset, int length, struct squashfs_page_actor *output);
80f784098ff44e Xiaoming Ni 2022-10-19 53 int (*max_decompressors)(void);
80f784098ff44e Xiaoming Ni 2022-10-19 54 };
80f784098ff44e Xiaoming Ni 2022-10-19 55 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Bo,
kernel test robot noticed the following build warnings:
[auto build test WARNING on v6.16-rc3]
[also build test WARNING on linus/master next-20250626]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Bo-Liu/Squashfs-add-page-cache-share-support/20250626-084010 base: v6.16-rc3 patch link: https://lore.kernel.org/r/20250626003644.3675-1-liubo03%40inspur.com patch subject: [PATCH] Squashfs: add page cache share support config: m68k-randconfig-r132-20250627 (https://download.01.org/0day-ci/archive/20250627/[email protected]/config) compiler: m68k-linux-gcc (GCC) 8.5.0 reproduce: (https://download.01.org/0day-ci/archive/20250627/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> fs/squashfs/pagecache_share.c:136:35: sparse: sparse: symbol 'squashfs_file_vm_ops' was not declared. Should it be static?
vim +/squashfs_file_vm_ops +136 fs/squashfs/pagecache_share.c
135
> 136 const struct vm_operations_struct squashfs_file_vm_ops = { 137 .fault = filemap_fault,
138 .map_pages = filemap_map_pages,
139 .page_mkwrite = filemap_page_mkwrite,
140 };
141
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Bo,
kernel test robot noticed the following build errors:
[auto build test ERROR on v6.16-rc3]
[also build test ERROR on linus/master next-20250627]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Bo-Liu/Squashfs-add-page-cache-share-support/20250626-084010 base: v6.16-rc3 patch link: https://lore.kernel.org/r/20250626003644.3675-1-liubo03%40inspur.com patch subject: [PATCH] Squashfs: add page cache share support config: sparc64-randconfig-r054-20250627 (https://download.01.org/0day-ci/archive/20250627/[email protected]/config) compiler: sparc64-linux-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250627/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from fs/squashfs/pagecache_share.c:16:
fs/squashfs/xattr.h: In function 'squashfs_read_xattr_id_table':
fs/squashfs/xattr.h:22:13: error: implicit declaration of function 'squashfs_read_table'; did you mean 'squashfs_read_xattr_id_table'? [-Werror=implicit-function-declaration]
id_table = squashfs_read_table(sb, start, sizeof(*id_table));
^~~~~~~~~~~~~~~~~~~
squashfs_read_xattr_id_table
>> fs/squashfs/xattr.h:22:51: error: dereferencing pointer to incomplete type 'struct squashfs_xattr_id_table'
id_table = squashfs_read_table(sb, start, sizeof(*id_table));
^~~~~~~~~
fs/squashfs/xattr.h:29:2: error: implicit declaration of function 'ERROR'; did you mean 'ERR_PTR'? [-Werror=implicit-function-declaration]
ERROR("Xattrs in filesystem, these will be ignored\n");
^~~~~
ERR_PTR
In file included from fs/squashfs/pagecache_share.c:18:
fs/squashfs/squashfs.h: At top level:
fs/squashfs/squashfs.h:25:12: warning: 'struct squashfs_page_actor' declared inside parameter list will not be visible outside of this definition or declaration
struct squashfs_page_actor *);
^~~~~~~~~~~~~~~~~~~
fs/squashfs/squashfs.h:40:14: error: conflicting types for 'squashfs_read_table'
extern void *squashfs_read_table(struct super_block *, u64, int);
^~~~~~~~~~~~~~~~~~~
In file included from fs/squashfs/pagecache_share.c:16:
fs/squashfs/xattr.h:22:13: note: previous implicit declaration of 'squashfs_read_table' was here
id_table = squashfs_read_table(sb, start, sizeof(*id_table));
^~~~~~~~~~~~~~~~~~~
In file included from fs/squashfs/pagecache_share.c:18:
fs/squashfs/squashfs.h:49:26: warning: 'struct squashfs_sb_info' declared inside parameter list will not be visible outside of this definition or declaration
void * (*create)(struct squashfs_sb_info *msblk, void *comp_opts);
^~~~~~~~~~~~~~~~
fs/squashfs/squashfs.h:50:25: warning: 'struct squashfs_sb_info' declared inside parameter list will not be visible outside of this definition or declaration
void (*destroy)(struct squashfs_sb_info *msblk);
^~~~~~~~~~~~~~~~
fs/squashfs/squashfs.h:52:37: warning: 'struct squashfs_page_actor' declared inside parameter list will not be visible outside of this definition or declaration
int offset, int length, struct squashfs_page_actor *output);
^~~~~~~~~~~~~~~~~~~
fs/squashfs/squashfs.h:51:27: warning: 'struct squashfs_sb_info' declared inside parameter list will not be visible outside of this definition or declaration
int (*decompress)(struct squashfs_sb_info *msblk, struct bio *bio,
^~~~~~~~~~~~~~~~
In file included from include/uapi/linux/posix_types.h:5,
from include/uapi/linux/types.h:14,
from include/linux/types.h:6,
from include/linux/kasan-checks.h:5,
from include/asm-generic/rwonce.h:26,
from ./arch/sparc/include/generated/asm/rwonce.h:1,
from include/linux/compiler.h:390,
from include/linux/build_bug.h:5,
from include/linux/bits.h:32,
from include/linux/bitops.h:6,
from include/linux/bitmap.h:8,
from include/linux/xarray.h:12,
from fs/squashfs/pagecache_share.c:5:
include/linux/stddef.h:8:16: error: expected identifier or '(' before 'void'
#define NULL ((void *)0)
^~~~
fs/squashfs/xattr.h:39:28: note: in expansion of macro 'NULL'
#define squashfs_listxattr NULL
^~~~
fs/squashfs/squashfs.h:93:16: note: in expansion of macro 'squashfs_listxattr'
extern ssize_t squashfs_listxattr(struct dentry *, char *, size_t);
^~~~~~~~~~~~~~~~~~
include/linux/stddef.h:8:23: error: expected ')' before numeric constant
#define NULL ((void *)0)
^
fs/squashfs/xattr.h:39:28: note: in expansion of macro 'NULL'
#define squashfs_listxattr NULL
^~~~
fs/squashfs/squashfs.h:93:16: note: in expansion of macro 'squashfs_listxattr'
extern ssize_t squashfs_listxattr(struct dentry *, char *, size_t);
^~~~~~~~~~~~~~~~~~
include/linux/stddef.h:8:16: error: expected identifier or '(' before 'void'
#define NULL ((void *)0)
^~~~
fs/squashfs/xattr.h:40:33: note: in expansion of macro 'NULL'
#define squashfs_xattr_handlers NULL
^~~~
fs/squashfs/squashfs.h:119:43: note: in expansion of macro 'squashfs_xattr_handlers'
extern const struct xattr_handler * const squashfs_xattr_handlers[];
^~~~~~~~~~~~~~~~~~~~~~~
include/linux/stddef.h:8:23: error: expected ')' before numeric constant
#define NULL ((void *)0)
^
fs/squashfs/xattr.h:40:33: note: in expansion of macro 'NULL'
#define squashfs_xattr_handlers NULL
^~~~
fs/squashfs/squashfs.h:119:43: note: in expansion of macro 'squashfs_xattr_handlers'
extern const struct xattr_handler * const squashfs_xattr_handlers[];
^~~~~~~~~~~~~~~~~~~~~~~cc1: some warnings being treated as errors
vim +22 fs/squashfs/xattr.h
f37aa4c7366e23f Phillip Lougher 2021-02-09 @22 id_table = squashfs_read_table(sb, start, sizeof(*id_table));
f37aa4c7366e23f Phillip Lougher 2021-02-09 23 if (IS_ERR(id_table))
f37aa4c7366e23f Phillip Lougher 2021-02-09 24 return (__le64 *) id_table;
f37aa4c7366e23f Phillip Lougher 2021-02-09 25
f37aa4c7366e23f Phillip Lougher 2021-02-09 26 *xattr_table_start = le64_to_cpu(id_table->xattr_table_start);
f37aa4c7366e23f Phillip Lougher 2021-02-09 27 kfree(id_table);
f37aa4c7366e23f Phillip Lougher 2021-02-09 28
01e5b4e4e897fce Phillip Lougher 2010-05-17 29 ERROR("Xattrs in filesystem, these will be ignored\n");
01e5b4e4e897fce Phillip Lougher 2010-05-17 30 return ERR_PTR(-ENOTSUPP);
01e5b4e4e897fce Phillip Lougher 2010-05-17 31 }
01e5b4e4e897fce Phillip Lougher 2010-05-17 32 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Bo,
kernel test robot noticed the following build warnings:
[auto build test WARNING on v6.16-rc3]
[also build test WARNING on linus/master next-20250627]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Bo-Liu/Squashfs-add-page-cache-share-support/20250626-084010 base: v6.16-rc3 patch link: https://lore.kernel.org/r/20250626003644.3675-1-liubo03%40inspur.com patch subject: [PATCH] Squashfs: add page cache share support config: hexagon-randconfig-r073-20250628 (https://download.01.org/0day-ci/archive/20250628/[email protected]/config) compiler: clang version 21.0.0git (https://github.com/llvm/llvm-project e04c938cc08a90ae60440ce22d072ebc69d67ee8)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
New smatch warnings:fs/squashfs/inode.c:97 squashfs_iget() warn: inconsistent indenting
Old smatch warnings:fs/squashfs/inode.c:293 squashfs_read_inode() warn: missing unwind goto?
vim +97 fs/squashfs/inode.c
74
75
76 struct inode *squashfs_iget(struct super_block *sb, long long ino,
77 unsigned int ino_number)
78 {
79 struct inode *inode = iget_locked(sb, ino_number);
80 int err;
81
82 TRACE("Entered squashfs_iget\n");
83
84 if (!inode)
85 return ERR_PTR(-ENOMEM);
86 if (!(inode->i_state & I_NEW))
87 return inode;
88
89 err = squashfs_read_inode(inode, ino);
90 if (err) {
91 iget_failed(inode);
92 return ERR_PTR(err);
93 }
94 unlock_new_inode(inode);
95
96 #ifdef CONFIG_SQUASHFS_PAGE_CACHE_SHARE
> 97 if ((inode->i_mode & S_IFMT) == S_IFREG) {
98 if (squashfs_pcs_fill_inode(inode) > 0)
99 inode->i_fop = &squashfs_pcs_file_fops; 100 }
101 #endif
102
103 return inode;
104 }
105
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Phillip Lougher
maintainerQuoted 6 lines · Bo Liu
Last year, Hongzhen Luo submitted the page cache sharing feature for the EROFS file system, highlighting that in container scenarios, different images mounted at separate mount points cause identical files to maintain duplicate page caches. This issue also exists in the SquashFS file system, so we added support for page cache sharing on SquashFS.
I have been expecting a V2 patch-set fixing the "kernel test robot" emails. But, as this hasn't arrived, I have decided to review this V1 patch-set.
There are a number of things which need to be fixed in a V2 patch-set, namely:
1. Some checkpatch.pl warnings. Please remember to run cripts/checkpatch.pl before sending the V2 patch-set.
2. Compiler warnings/errors which cause build failure
3. A potential NULL pointer dereference
4. Typing and indentation mistakes etc.
These issues are mentioned inline below.
Quoted 9 lines
To enable page cache sharing, the extended attribute of each file must be configured. This involves calculating the file's MD5 value and setting it via the setfattr command.
# md5sum $file # setfattr -n "trusted.md5sum" -v "$hash" $fileA 300MB file was packaged into two separate images, which were mounted at different mount points. fio was used to read the file contents, comparing the system page cache size with and without the page cache sharing feature enabled.
checkpatch.pl complains that the above lines exceed 75 characters per line.
WARNING: Prefer a maximum 75 chars per line (possible unwrapped commit description?)
Quoted 28 lines
|---------------|-------------------------|--------------------------| | | enable page cache share | disable page cache share | |---------------|-------------------------|--------------------------| |page cache size| 501MiB | 1052MiB | |---------------|-------------------------|--------------------------|Signed-off-by Bo Liu <[email protected]>--- fs/squashfs/Kconfig | 10 +++ fs/squashfs/Makefile | 1 + fs/squashfs/inode.c | 10 ++- fs/squashfs/pagecache_share.c | 159 ++++++++++++++++++++++++++++++++++ fs/squashfs/pagecache_share.h | 22 +++++ fs/squashfs/squashfs.h | 2 + fs/squashfs/squashfs_fs_i.h | 5 ++ fs/squashfs/super.c | 45 +++++++++- 8 files changed, 252 insertions(+), 2 deletions(-)create mode 100644
fs/squashfs/pagecache_share.ccreate mode 100644fs/squashfs/pagecache_share.hfs/squashfs/Kconfig +0
-0@@ -285,3 +285,13 @@ config SQUASHFS_FRAGMENT_CACHE_SIZE Note there must be at least one cached fragment. Anything
fs/squashfs/Kconfig config SQUASHFS_FRAGMENT_CACHE_SIZE · line 296
Quoted 10 lines
fs/squashfs/Kconfig +9
-0much more than three will probably not make much difference. + +config SQUASHFS_PAGE_CACHE_SHARE + bool "SQUASHFS page cache share support" + depends on SQUASHFS + default n + help + Saying Y here includes support for permiting SQUASHFS to share + page cache for files with same fingerprints. +
Two issues:
1. Page cache sharing relies on Squashfs XATTR support being built. If it isn't not only won't any sharing happen, but, squashfs_pcs_fill_inode() will fault dereferencing a NULL pointer.
Thus this option should explictly select SQUASHFS_XATTR. Or this option should be made to depend on SQUASHFS_XATTR.
2. checkpatch.pl complains that the above configuration description is insufficent.
WARNING: please write a help paragraph that fully describes the config symbol with at least 4 lines
You could add your description of how to enable page cache sharing, e.g.
"To enable page cache sharing, the extended attribute of each file must be configured. This involves calculating the file's MD5 value and setting it via the setfattr command.
# md5sum $file
# setfattr -n "trusted.md5sum" -v "$hash" $file"Quoted 25 lines
fs/squashfs/Kconfig +1
-0+ If unsure, say N.fs/squashfs/Makefile +1
-0@@ -17,3 +17,4 @@ squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o squashfs-$(CONFIG_SQUASHFS_XZ) += xz_wrapper.o squashfs-$(CONFIG_SQUASHFS_ZLIB) += zlib_wrapper.o squashfs-$(CONFIG_SQUASHFS_ZSTD) += zstd_wrapper.o +squashfs-$(CONFIG_SQUASHFS_PAGE_CACHE_SHARE) += pagecache_share.ofs/squashfs/inode.c +1
-0@@ -35,6 +35,7 @@#include "squashfs_fs_i.h" #include "squashfs.h" #include "xattr.h" +#include "pagecache_share.h" /* * Initialise VFS inode with the base inode information common to all @@ -90,8 +91,15 @@ struct inode *squashfs_iget(struct super_block *sb, long long ino,iget_failed (inode);return ERR_PTR (err);
fs/squashfs/inode.c struct inode *squashfs_iget(struct super_block *sb, long long ino, · line 101
Quoted 10 lines
fs/squashfs/inode.c +7
-1} -unlock_new_inode (inode); + +#ifdef CONFIG_SQUASHFS_PAGE_CACHE_SHARE +if ((inode->i_mode &S_IFMT ) ==S_IFREG ) { +if (squashfs_pcs_fill_inode (inode) >0 ) + inode->i_fop = &squashfs_pcs_file_fops; + } +#endif
The above code block is indented wrongly, with an extra tab.
Quoted 19 lines
fs/squashfs/inode.c +1
-0+return inode; }fs/squashfs/pagecache_share.c new file +9
-0@@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2024, Inspur +*/ +#include <linux/xarray.h> +#include <linux/mutex.h> +#include <linux/xxhash.h> +#include <linux/slab.h> +#include <linux/file.h>
fs/squashfs/pagecache_share.c line 19
Quoted 10 lines
fs/squashfs/pagecache_share.c new file +10
-0+#include <linux/fs.h> +#include <linux/mm.h> +#include <linux/xattr.h> +#include <linux/uio.h> +#include <uapi/linux/fcntl.h> +#include "squashfs_fs_i.h" +#include "xattr.h" +#include "pagecache_share.h" +#include "squashfs.h" +
This file generates a lot of compiler warnings and then the compiler aborts with a build failure.
This is caused by missing #includes.
You should replace
#include "squashfs_fs_i.h"
With
#include "squashfs_fs.h"
#include "squashfs_fs_sb.h"
#include "squashfs_fs_i.h"
#include "squashfs.h"
Quoted 33 lines
fs/squashfs/pagecache_share.c new file +33
-0+#define PCS_FPRT_NAME "md5sum" +#define PCS_FPRT_MAXLEN 64 + +static struct vfsmount *squashfs_pcs_mnt; + +int squashfs_pcs_init_mnt (void ) +{ +struct vfsmount *mnt; + + mnt =kern_mount (&squashfs_anon_fs_type); +if (IS_ERR (mnt)) +return PTR_ERR (mnt); + squashfs_pcs_mnt = mnt; +return 0 ; +} + +void squashfs_pcs_mnt_exit (void ) +{ +kern_unmount (squashfs_pcs_mnt); + squashfs_pcs_mnt =NULL ; +} + +static int squashfs_pcs_eq (struct inode *inode,void *data) +{ +return *(unsigned long *)(inode->i_private) == *(unsigned long *)data ?1 :0 ; +} + +static int squashfs_pcs_inode_set (struct inode *inode,void *data) +{ + inode->i_private =kmalloc (sizeof (unsigned long ),GFP_KERNEL ); + *(unsigned long *)(inode->i_private) = *(unsigned long *)data; +return 0 ; +}
fs/squashfs/pagecache_share.c line 62
Quoted 10 lines
fs/squashfs/pagecache_share.c new file +10
-0+ +int squashfs_pcs_fill_inode (struct inode *inode) +{ +struct squashfs_inode_info *sqi =squashfs_i (inode); +struct super_block *sb = inode->i_sb; +struct inode *ano_inode; +char fprt[PCS_FPRT_MAXLEN ]; +int fprt_len; +const struct xattr_handler *handler = sb->s_xattr[1 ]; +
If SQUASHFS_XATTR is not configured then sb->s_xattr will be NULL.
Quoted 105 lines
fs/squashfs/pagecache_share.c new file +97
-0+ fprt_len = handler->get (handler,NULL , inode,PCS_FPRT_NAME , + fprt,PCS_FPRT_MAXLEN ); +if (fprt_len <0 || fprt_len >PCS_FPRT_MAXLEN ) +return -EINVAL ; + + sqi->fprt_hash =xxh32 (fprt, fprt_len,0 ); + ano_inode =iget5_locked (squashfs_pcs_mnt->mnt_sb, + sqi->fprt_hash, squashfs_pcs_eq, + squashfs_pcs_inode_set, &sqi->fprt_hash); +if (IS_ERR (ano_inode)) +return -ENOMEM ; + +if (ano_inode->i_state &I_NEW ) { + ano_inode->i_mapping = inode->i_mapping; + ano_inode->i_size = inode->i_size; + ano_inode->i_data.a_ops = &squashfs_aops; +unlock_new_inode (ano_inode); + } + sqi->pcs_inode = ano_inode; +return fprt_len; +} + +static int squashfs_pcs_file_open (struct inode *inode,struct file *file) +{ +struct squashfs_inode_info *sqi =squashfs_i (inode); +struct inode *pcs_inode; +struct file *ano_file; + + pcs_inode = sqi->pcs_inode; +if (!pcs_inode) +return -EINVAL ; + + ano_file =alloc_file_pseudo (pcs_inode, squashfs_pcs_mnt, +"[squashfs_pcs_f]" ,O_RDONLY , + &generic_ro_fops); +if (!ano_file) +return -ENOMEM ; + +file_ra_state_init (&ano_file->f_ra, file->f_mapping); + file->private_data = (void *)ano_file; + ano_file->private_data =squashfs_i (inode); +return 0 ; +} + +static int squashfs_pcs_file_release (struct inode *inode,struct file *file) +{ +if (!file->private_data) +return -EINVAL ; +fput ((struct file *)file->private_data); + file->private_data =NULL ; + +return 0 ; +} + +static ssize_t squashfs_pcs_file_read_iter (struct kiocb *iocb, +struct iov_iter *iter) +{ +size_t count =iov_iter_count (iter); +struct file *backing_file = iocb->ki_filp->private_data; +struct kiocb dedup_iocb; +ssize_t nread; + +if (!count) +return 0 ; + +kiocb_clone (&dedup_iocb, iocb, backing_file); + nread =filemap_read (&dedup_iocb, iter,0 ); + iocb->ki_pos = dedup_iocb.ki_pos; +touch_atime (&iocb->ki_filp->f_path); + +return nread; +} + +const struct vm_operations_struct squashfs_file_vm_ops = { + .fault = filemap_fault, + .map_pages = filemap_map_pages, + .page_mkwrite = filemap_page_mkwrite, +}; + +static int squashfs_pcs_mmap (struct file *file,struct vm_area_struct *vma) +{ +struct file *ano_file = file->private_data; + +vma_set_file (vma, ano_file); + vma->vm_ops = &squashfs_file_vm_ops; +return 0 ; +} + +const struct file_operations squashfs_pcs_file_fops = { + .open = squashfs_pcs_file_open, + .llseek = generic_file_llseek, + .read_iter = squashfs_pcs_file_read_iter, + .mmap = squashfs_pcs_mmap, + .release = squashfs_pcs_file_release, + .get_unmapped_area = thp_get_unmapped_area, + .splice_read = filemap_splice_read, +};fs/squashfs/pagecache_share.h new file +2
-0@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/*
fs/squashfs/pagecache_share.h line 12
Quoted 10 lines
fs/squashfs/pagecache_share.h new file +10
-0+* Copyright (C) 2024, Inspur +*/ +#ifndef __SQUASHFS_PAGECACHE_SHARE_H +#define __SQUASHFS_PAGECACHE_SHARE_H + +#include <linux/mutex.h> +#include <linux/fs.h> +#include <linux/mount.h> +#include <linux/rwlock.h> +#include <linux/mutex.h>
Repeated inclusion of <linux/mutex.h>
fs/squashfs/pagecache_share.h line 15
Quoted 3 lines
fs/squashfs/pagecache_share.h new file +3
-0+ +int squashfs_pcs_fill_inode (struct inode *inode); +int sqyashfs_pcs_remove (struct inode *inode);
Mistyped forward declaration and the forward declaration is obviously not required.
Quoted 124 lines
fs/squashfs/pagecache_share.h new file +7
-0+int squashfs_pcs_init_mnt (void ); +void squashfs_pcs_mnt_exit (void ); + +extern const struct file_operations squashfs_pcs_file_fops; +extern const struct vm_operations_struct generic_file_vm_ops; +#endif +fs/squashfs/squashfs.h +2
-0@@ -117,3 +117,5 @@ extern const struct inode_operations squashfs_symlink_inode_ops;/* xattr.c */ extern const struct xattr_handler *const squashfs_xattr_handlers[]; + +extern struct file_system_type squashfs_anon_fs_type;fs/squashfs/squashfs_fs_i.h +5
-0@@ -30,6 +30,11 @@ struct squashfs_inode_info {int parent; }; }; + +#ifdef CONFIG_SQUASHFS_PAGE_CACHE_SHARE +unsigned long fprt_hash; +struct inode *pcs_inode; +#endif struct inode vfs_inode; };fs/squashfs/super.c +44
-1@@ -29,6 +29,7 @@#include <linux/module.h> #include <linux/magic.h> #include <linux/xattr.h> +#include <linux/pseudo_fs.h> #include "squashfs_fs.h" #include "squashfs_fs_sb.h" @@ -36,6 +37,7 @@#include "squashfs.h" #include "decompressor.h" #include "xattr.h" +#include "pagecache_share.h" static struct file_system_type squashfs_fs_type;static const struct super_operations squashfs_super_ops;@@ -654,6 +656,15 @@ static int __init init_squashfs_fs(void)return err; } +#ifdef CONFIG_SQUASHFS_PAGE_CACHE_SHARE + err =squashfs_pcs_init_mnt (); +if (err) { +destroy_inodecache (); +unregister_filesystem (&squashfs_fs_type); +return err; + } +#endif +pr_info ("version 4.0 (2009/01/31) Phillip Lougher\n" );return 0 ;@@ -662,6 +673,9 @@ static int __init init_squashfs_fs(void) static void __exit exit_squashfs_fs(void) { +#ifdef CONFIG_SQUASHFS_PAGE_CACHE_SHARE +squashfs_pcs_mnt_exit (); +#endif unregister_filesystem (&squashfs_fs_type);destroy_inodecache (); }@@ -675,7 +689,6 @@ static struct inode *squashfs_alloc_inode(struct super_block *sb)return ei ? &ei->vfs_inode :NULL ; } -static void squashfs_free_inode (struct inode *inode) {kmem_cache_free (squashfs_inode_cachep,squashfs_i (inode));@@ -698,6 +711,36 @@ static const struct super_operations squashfs_super_ops = { .put_super = squashfs_put_super, .show_options = squashfs_show_options, }; +#ifdef CONFIG_SQUASHFS_PAGE_CACHE_SHARE +static void squashfs_free_anon_inode (struct inode *inode) +{ +kfree (inode->i_private); +iput (inode); +} +#endif + +static const struct super_operations squashfs_anon_sops = { + .statfs = simple_statfs, +#ifdef CONFIG_SQUASHFS_PAGE_CACHE_SHARE + .free_inode = squashfs_free_anon_inode, +#endif +}; + +static int squashfs_anon_init_fs_context (struct fs_context *fc) +{ +struct pseudo_fs_context *ctx =init_pseudo (fc,SQUASHFS_MAGIC ); + +if (ctx) + ctx->ops = &squashfs_anon_sops; +return ctx ?0 : -ENOMEM ; +} + +struct file_system_type squashfs_anon_fs_type = { + .owner =THIS_MODULE , + .name ="pseudo_squashfs" , + .init_fs_context = squashfs_anon_init_fs_context, + .kill_sb = kill_anon_super, +};module_init (init_squashfs_fs);module_exit (exit_squashfs_fs);