xref: /openbmc/linux/fs/erofs/internal.h (revision 6a318ccd)
147e4937aSGao Xiang /* SPDX-License-Identifier: GPL-2.0-only */
247e4937aSGao Xiang /*
347e4937aSGao Xiang  * Copyright (C) 2017-2018 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov  *             https://www.huawei.com/
5c5aa903aSGao Xiang  * Copyright (C) 2021, Alibaba Cloud
647e4937aSGao Xiang  */
747e4937aSGao Xiang #ifndef __EROFS_INTERNAL_H
847e4937aSGao Xiang #define __EROFS_INTERNAL_H
947e4937aSGao Xiang 
1047e4937aSGao Xiang #include <linux/fs.h>
1147e4937aSGao Xiang #include <linux/dcache.h>
1247e4937aSGao Xiang #include <linux/mm.h>
1347e4937aSGao Xiang #include <linux/pagemap.h>
1447e4937aSGao Xiang #include <linux/bio.h>
1547e4937aSGao Xiang #include <linux/magic.h>
1647e4937aSGao Xiang #include <linux/slab.h>
1747e4937aSGao Xiang #include <linux/vmalloc.h>
18eadcd6b5SGao Xiang #include <linux/iomap.h>
1947e4937aSGao Xiang #include "erofs_fs.h"
2047e4937aSGao Xiang 
2147e4937aSGao Xiang /* redefine pr_fmt "erofs: " */
2247e4937aSGao Xiang #undef pr_fmt
2347e4937aSGao Xiang #define pr_fmt(fmt) "erofs: " fmt
2447e4937aSGao Xiang 
254f761fa2SGao Xiang __printf(3, 4) void _erofs_err(struct super_block *sb,
264f761fa2SGao Xiang 			       const char *function, const char *fmt, ...);
274f761fa2SGao Xiang #define erofs_err(sb, fmt, ...)	\
284f761fa2SGao Xiang 	_erofs_err(sb, __func__, fmt "\n", ##__VA_ARGS__)
294f761fa2SGao Xiang __printf(3, 4) void _erofs_info(struct super_block *sb,
304f761fa2SGao Xiang 			       const char *function, const char *fmt, ...);
314f761fa2SGao Xiang #define erofs_info(sb, fmt, ...) \
324f761fa2SGao Xiang 	_erofs_info(sb, __func__, fmt "\n", ##__VA_ARGS__)
3347e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_DEBUG
344f761fa2SGao Xiang #define erofs_dbg(x, ...)       pr_debug(x "\n", ##__VA_ARGS__)
3547e4937aSGao Xiang #define DBG_BUGON               BUG_ON
3647e4937aSGao Xiang #else
374f761fa2SGao Xiang #define erofs_dbg(x, ...)       ((void)0)
3847e4937aSGao Xiang #define DBG_BUGON(x)            ((void)(x))
3947e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_DEBUG */
4047e4937aSGao Xiang 
4147e4937aSGao Xiang /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
4247e4937aSGao Xiang #define EROFS_SUPER_MAGIC   EROFS_SUPER_MAGIC_V1
4347e4937aSGao Xiang 
4447e4937aSGao Xiang typedef u64 erofs_nid_t;
4547e4937aSGao Xiang typedef u64 erofs_off_t;
4647e4937aSGao Xiang /* data type for filesystem-wide blocks number */
4747e4937aSGao Xiang typedef u32 erofs_blk_t;
4847e4937aSGao Xiang 
49dfeab2e9SGao Xiang struct erofs_device_info {
50dfeab2e9SGao Xiang 	char *path;
51955b478eSJeffle Xu 	struct erofs_fscache *fscache;
52dfeab2e9SGao Xiang 	struct block_device *bdev;
53dfeab2e9SGao Xiang 	struct dax_device *dax_dev;
54cd913c76SChristoph Hellwig 	u64 dax_part_off;
55dfeab2e9SGao Xiang 
56dfeab2e9SGao Xiang 	u32 blocks;
57dfeab2e9SGao Xiang 	u32 mapped_blkaddr;
58dfeab2e9SGao Xiang };
59dfeab2e9SGao Xiang 
6040452ffcSHuang Jianan enum {
6140452ffcSHuang Jianan 	EROFS_SYNC_DECOMPRESS_AUTO,
6240452ffcSHuang Jianan 	EROFS_SYNC_DECOMPRESS_FORCE_ON,
6340452ffcSHuang Jianan 	EROFS_SYNC_DECOMPRESS_FORCE_OFF
6440452ffcSHuang Jianan };
6540452ffcSHuang Jianan 
66e6242465SGao Xiang struct erofs_mount_opts {
67f57a3fe4SChao Yu #ifdef CONFIG_EROFS_FS_ZIP
68f57a3fe4SChao Yu 	/* current strategy of how to use managed cache */
69f57a3fe4SChao Yu 	unsigned char cache_strategy;
7040452ffcSHuang Jianan 	/* strategy of sync decompression (0 - auto, 1 - force on, 2 - force off) */
7140452ffcSHuang Jianan 	unsigned int sync_decompress;
72f57a3fe4SChao Yu 
73f57a3fe4SChao Yu 	/* threshold for decompression synchronously */
74f57a3fe4SChao Yu 	unsigned int max_sync_decompress_pages;
75f57a3fe4SChao Yu #endif
76f57a3fe4SChao Yu 	unsigned int mount_opt;
77f57a3fe4SChao Yu };
78f57a3fe4SChao Yu 
79dfeab2e9SGao Xiang struct erofs_dev_context {
80dfeab2e9SGao Xiang 	struct idr tree;
81dfeab2e9SGao Xiang 	struct rw_semaphore rwsem;
82dfeab2e9SGao Xiang 
83dfeab2e9SGao Xiang 	unsigned int extra_devices;
848b465fecSJia Zhu 	bool flatdev;
85dfeab2e9SGao Xiang };
86dfeab2e9SGao Xiang 
87e6242465SGao Xiang struct erofs_fs_context {
88e6242465SGao Xiang 	struct erofs_mount_opts opt;
89dfeab2e9SGao Xiang 	struct erofs_dev_context *devs;
9039bfcb81SJingbo Xu 	char *fsid;
9139bfcb81SJingbo Xu 	char *domain_id;
92e6242465SGao Xiang };
93e6242465SGao Xiang 
945d50538fSHuang Jianan /* all filesystem-wide lz4 configurations */
955d50538fSHuang Jianan struct erofs_sb_lz4_info {
965d50538fSHuang Jianan 	/* # of pages needed for EROFS lz4 rolling decompression */
975d50538fSHuang Jianan 	u16 max_distance_pages;
984fea63f7SGao Xiang 	/* maximum possible blocks for pclusters in the filesystem */
994fea63f7SGao Xiang 	u16 max_pclusterblks;
1005d50538fSHuang Jianan };
1015d50538fSHuang Jianan 
1028b7adf1dSJia Zhu struct erofs_domain {
1038b7adf1dSJia Zhu 	refcount_t ref;
1048b7adf1dSJia Zhu 	struct list_head list;
1058b7adf1dSJia Zhu 	struct fscache_volume *volume;
1068b7adf1dSJia Zhu 	char *domain_id;
1078b7adf1dSJia Zhu };
1088b7adf1dSJia Zhu 
109b02c602fSJeffle Xu struct erofs_fscache {
110b02c602fSJeffle Xu 	struct fscache_cookie *cookie;
11161fef989SJingbo Xu 	struct inode *inode;	/* anonymous inode for the blob */
1122dfb8c3bSJingbo Xu 
1132dfb8c3bSJingbo Xu 	/* used for share domain mode */
1147d419637SJia Zhu 	struct erofs_domain *domain;
1152dfb8c3bSJingbo Xu 	struct list_head node;
1162dfb8c3bSJingbo Xu 	refcount_t ref;
1177d419637SJia Zhu 	char *name;
118b02c602fSJeffle Xu };
119b02c602fSJeffle Xu 
1209e382914SJingbo Xu struct erofs_xattr_prefix_item {
1219e382914SJingbo Xu 	struct erofs_xattr_long_prefix *prefix;
1229e382914SJingbo Xu 	u8 infix_len;
1239e382914SJingbo Xu };
1249e382914SJingbo Xu 
12547e4937aSGao Xiang struct erofs_sb_info {
126e6242465SGao Xiang 	struct erofs_mount_opts opt;	/* options */
12747e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
12847e4937aSGao Xiang 	/* list for all registered superblocks, mainly for shrinker */
12947e4937aSGao Xiang 	struct list_head list;
13047e4937aSGao Xiang 	struct mutex umount_mutex;
13147e4937aSGao Xiang 
13264094a04SGao Xiang 	/* managed XArray arranged in physical block number */
13364094a04SGao Xiang 	struct xarray managed_pslots;
13447e4937aSGao Xiang 
13547e4937aSGao Xiang 	unsigned int shrinker_run_no;
13614373711SGao Xiang 	u16 available_compr_algs;
13747e4937aSGao Xiang 
13847e4937aSGao Xiang 	/* pseudo inode to manage cached pages */
13947e4937aSGao Xiang 	struct inode *managed_cache;
1405d50538fSHuang Jianan 
1415d50538fSHuang Jianan 	struct erofs_sb_lz4_info lz4;
14247e4937aSGao Xiang #endif	/* CONFIG_EROFS_FS_ZIP */
143a97a218bSJingbo Xu 	struct inode *packed_inode;
144dfeab2e9SGao Xiang 	struct erofs_dev_context *devs;
14506252e9cSGao Xiang 	struct dax_device *dax_dev;
146cd913c76SChristoph Hellwig 	u64 dax_part_off;
147dfeab2e9SGao Xiang 	u64 total_blocks;
148dfeab2e9SGao Xiang 	u32 primarydevice_blocks;
149dfeab2e9SGao Xiang 
15047e4937aSGao Xiang 	u32 meta_blkaddr;
15147e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_XATTR
15247e4937aSGao Xiang 	u32 xattr_blkaddr;
1539e382914SJingbo Xu 	u32 xattr_prefix_start;
1549e382914SJingbo Xu 	u8 xattr_prefix_count;
1559e382914SJingbo Xu 	struct erofs_xattr_prefix_item *xattr_prefixes;
15647e4937aSGao Xiang #endif
157dfeab2e9SGao Xiang 	u16 device_id_mask;	/* valid bits of device id to be used */
15847e4937aSGao Xiang 
1593acea5fcSJingbo Xu 	unsigned char islotbits;	/* inode slot unit size in bit shift */
160d3c4bdccSJingbo Xu 	unsigned char blkszbits;	/* filesystem block size in bit shift */
16147e4937aSGao Xiang 
16214373711SGao Xiang 	u32 sb_size;			/* total superblock size */
16347e4937aSGao Xiang 	u32 build_time_nsec;
16447e4937aSGao Xiang 	u64 build_time;
16547e4937aSGao Xiang 
16647e4937aSGao Xiang 	/* what we really care is nid, rather than ino.. */
16747e4937aSGao Xiang 	erofs_nid_t root_nid;
168cb9bce79SJingbo Xu 	erofs_nid_t packed_nid;
16947e4937aSGao Xiang 	/* used for statfs, f_files - f_favail */
17047e4937aSGao Xiang 	u64 inos;
17147e4937aSGao Xiang 
17247e4937aSGao Xiang 	u8 uuid[16];                    /* 128-bit uuid for volume */
17347e4937aSGao Xiang 	u8 volume_name[16];             /* volume name */
174b858a484SPratik Shinde 	u32 feature_compat;
175426a9308SGao Xiang 	u32 feature_incompat;
176168e9a76SHuang Jianan 
177168e9a76SHuang Jianan 	/* sysfs support */
178168e9a76SHuang Jianan 	struct kobject s_kobj;		/* /sys/fs/erofs/<devname> */
179168e9a76SHuang Jianan 	struct completion s_kobj_unregister;
180c6be2bd0SJeffle Xu 
181c6be2bd0SJeffle Xu 	/* fscache support */
182c6be2bd0SJeffle Xu 	struct fscache_volume *volume;
18337c90c5fSJeffle Xu 	struct erofs_fscache *s_fscache;
1848b7adf1dSJia Zhu 	struct erofs_domain *domain;
18539bfcb81SJingbo Xu 	char *fsid;
18639bfcb81SJingbo Xu 	char *domain_id;
18747e4937aSGao Xiang };
18847e4937aSGao Xiang 
18947e4937aSGao Xiang #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
19047e4937aSGao Xiang #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
19147e4937aSGao Xiang 
19247e4937aSGao Xiang /* Mount flags set via mount options or defaults */
19347e4937aSGao Xiang #define EROFS_MOUNT_XATTR_USER		0x00000010
19447e4937aSGao Xiang #define EROFS_MOUNT_POSIX_ACL		0x00000020
19506252e9cSGao Xiang #define EROFS_MOUNT_DAX_ALWAYS		0x00000040
19606252e9cSGao Xiang #define EROFS_MOUNT_DAX_NEVER		0x00000080
19747e4937aSGao Xiang 
198e6242465SGao Xiang #define clear_opt(opt, option)	((opt)->mount_opt &= ~EROFS_MOUNT_##option)
199e6242465SGao Xiang #define set_opt(opt, option)	((opt)->mount_opt |= EROFS_MOUNT_##option)
200e6242465SGao Xiang #define test_opt(opt, option)	((opt)->mount_opt & EROFS_MOUNT_##option)
20147e4937aSGao Xiang 
20293b856bbSJeffle Xu static inline bool erofs_is_fscache_mode(struct super_block *sb)
20393b856bbSJeffle Xu {
20493b856bbSJeffle Xu 	return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && !sb->s_bdev;
20593b856bbSJeffle Xu }
20693b856bbSJeffle Xu 
20747e4937aSGao Xiang enum {
20847e4937aSGao Xiang 	EROFS_ZIP_CACHE_DISABLED,
20947e4937aSGao Xiang 	EROFS_ZIP_CACHE_READAHEAD,
21047e4937aSGao Xiang 	EROFS_ZIP_CACHE_READAROUND
21147e4937aSGao Xiang };
21247e4937aSGao Xiang 
21347e4937aSGao Xiang #define EROFS_LOCKED_MAGIC     (INT_MIN | 0xE0F510CCL)
21447e4937aSGao Xiang 
21547e4937aSGao Xiang /* basic unit of the workstation of a super_block */
21647e4937aSGao Xiang struct erofs_workgroup {
21747e4937aSGao Xiang 	/* the workgroup index in the workstation */
21847e4937aSGao Xiang 	pgoff_t index;
21947e4937aSGao Xiang 
22047e4937aSGao Xiang 	/* overall workgroup reference count */
22147e4937aSGao Xiang 	atomic_t refcount;
22247e4937aSGao Xiang };
22347e4937aSGao Xiang 
22447e4937aSGao Xiang static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
22547e4937aSGao Xiang 						 int val)
22647e4937aSGao Xiang {
22747e4937aSGao Xiang 	preempt_disable();
22847e4937aSGao Xiang 	if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
22947e4937aSGao Xiang 		preempt_enable();
23047e4937aSGao Xiang 		return false;
23147e4937aSGao Xiang 	}
23247e4937aSGao Xiang 	return true;
23347e4937aSGao Xiang }
23447e4937aSGao Xiang 
23547e4937aSGao Xiang static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
23647e4937aSGao Xiang 					    int orig_val)
23747e4937aSGao Xiang {
23847e4937aSGao Xiang 	/*
23947e4937aSGao Xiang 	 * other observers should notice all modifications
24047e4937aSGao Xiang 	 * in the freezing period.
24147e4937aSGao Xiang 	 */
24247e4937aSGao Xiang 	smp_mb();
24347e4937aSGao Xiang 	atomic_set(&grp->refcount, orig_val);
24447e4937aSGao Xiang 	preempt_enable();
24547e4937aSGao Xiang }
24647e4937aSGao Xiang 
24747e4937aSGao Xiang static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
24847e4937aSGao Xiang {
24947e4937aSGao Xiang 	return atomic_cond_read_relaxed(&grp->refcount,
25047e4937aSGao Xiang 					VAL != EROFS_LOCKED_MAGIC);
25147e4937aSGao Xiang }
25247e4937aSGao Xiang 
253fdf80a47SGao Xiang enum erofs_kmap_type {
254fdf80a47SGao Xiang 	EROFS_NO_KMAP,		/* don't map the buffer */
255927e5010SGao Xiang 	EROFS_KMAP,		/* use kmap_local_page() to map the buffer */
256fdf80a47SGao Xiang };
257fdf80a47SGao Xiang 
258fdf80a47SGao Xiang struct erofs_buf {
259eb2c5e41SGao Xiang 	struct inode *inode;
260fdf80a47SGao Xiang 	struct page *page;
261fdf80a47SGao Xiang 	void *base;
262fdf80a47SGao Xiang 	enum erofs_kmap_type kmap_type;
263fdf80a47SGao Xiang };
264fdf80a47SGao Xiang #define __EROFS_BUF_INITIALIZER	((struct erofs_buf){ .page = NULL })
265fdf80a47SGao Xiang 
26647e4937aSGao Xiang #define ROOT_NID(sb)		((sb)->root_nid)
26747e4937aSGao Xiang 
2683acea5fcSJingbo Xu #define erofs_blknr(sb, addr)	((addr) >> (sb)->s_blocksize_bits)
2693acea5fcSJingbo Xu #define erofs_blkoff(sb, addr)	((addr) & ((sb)->s_blocksize - 1))
2703acea5fcSJingbo Xu #define erofs_pos(sb, blk)	((erofs_off_t)(blk) << (sb)->s_blocksize_bits)
2713acea5fcSJingbo Xu #define erofs_iblks(i)	(round_up((i)->i_size, i_blocksize(i)) >> (i)->i_blkbits)
27247e4937aSGao Xiang 
273de06a6a3SGao Xiang #define EROFS_FEATURE_FUNCS(name, compat, feature) \
274de06a6a3SGao Xiang static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
275de06a6a3SGao Xiang { \
276de06a6a3SGao Xiang 	return sbi->feature_##compat & EROFS_FEATURE_##feature; \
277de06a6a3SGao Xiang }
278de06a6a3SGao Xiang 
2797e508f2cSHuang Jianan EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
28014373711SGao Xiang EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
2815404c330SGao Xiang EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
282168e9a76SHuang Jianan EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
283dfeab2e9SGao Xiang EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
284168e9a76SHuang Jianan EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
285ab92184fSYue Hu EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
286b15b2e30SYue Hu EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
2875c2a6425SGao Xiang EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
288*6a318ccdSJingbo Xu EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
289de06a6a3SGao Xiang EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
290de06a6a3SGao Xiang 
29147e4937aSGao Xiang /* atomic flag definitions */
292a5876e24SGao Xiang #define EROFS_I_EA_INITED_BIT	0
293a5876e24SGao Xiang #define EROFS_I_Z_INITED_BIT	1
29447e4937aSGao Xiang 
29547e4937aSGao Xiang /* bitlock definitions (arranged in reverse order) */
296a5876e24SGao Xiang #define EROFS_I_BL_XATTR_BIT	(BITS_PER_LONG - 1)
297a5876e24SGao Xiang #define EROFS_I_BL_Z_BIT	(BITS_PER_LONG - 2)
29847e4937aSGao Xiang 
299a5876e24SGao Xiang struct erofs_inode {
30047e4937aSGao Xiang 	erofs_nid_t nid;
30147e4937aSGao Xiang 
30247e4937aSGao Xiang 	/* atomic flags (including bitlocks) */
30347e4937aSGao Xiang 	unsigned long flags;
30447e4937aSGao Xiang 
3058a765682SGao Xiang 	unsigned char datalayout;
30647e4937aSGao Xiang 	unsigned char inode_isize;
30747e4937aSGao Xiang 	unsigned short xattr_isize;
30847e4937aSGao Xiang 
30947e4937aSGao Xiang 	unsigned int xattr_shared_count;
31047e4937aSGao Xiang 	unsigned int *xattr_shared_xattrs;
31147e4937aSGao Xiang 
31247e4937aSGao Xiang 	union {
31347e4937aSGao Xiang 		erofs_blk_t raw_blkaddr;
314c5aa903aSGao Xiang 		struct {
315c5aa903aSGao Xiang 			unsigned short	chunkformat;
316c5aa903aSGao Xiang 			unsigned char	chunkbits;
317c5aa903aSGao Xiang 		};
31847e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
31947e4937aSGao Xiang 		struct {
32047e4937aSGao Xiang 			unsigned short z_advise;
32147e4937aSGao Xiang 			unsigned char  z_algorithmtype[2];
32247e4937aSGao Xiang 			unsigned char  z_logical_clusterbits;
323ab92184fSYue Hu 			unsigned long  z_tailextent_headlcn;
324b15b2e30SYue Hu 			union {
325b15b2e30SYue Hu 				struct {
32622ba5e99SGao Xiang 					erofs_off_t    z_idataoff;
327ab92184fSYue Hu 					unsigned short z_idata_size;
32847e4937aSGao Xiang 				};
329b15b2e30SYue Hu 				erofs_off_t z_fragmentoff;
330b15b2e30SYue Hu 			};
331b15b2e30SYue Hu 		};
33247e4937aSGao Xiang #endif	/* CONFIG_EROFS_FS_ZIP */
33347e4937aSGao Xiang 	};
33447e4937aSGao Xiang 	/* the corresponding vfs inode */
33547e4937aSGao Xiang 	struct inode vfs_inode;
33647e4937aSGao Xiang };
33747e4937aSGao Xiang 
338b780d3fcSGao Xiang #define EROFS_I(ptr)	container_of(ptr, struct erofs_inode, vfs_inode)
33947e4937aSGao Xiang 
340b780d3fcSGao Xiang static inline erofs_off_t erofs_iloc(struct inode *inode)
34147e4937aSGao Xiang {
342b780d3fcSGao Xiang 	struct erofs_sb_info *sbi = EROFS_I_SB(inode);
343b780d3fcSGao Xiang 
3443acea5fcSJingbo Xu 	return erofs_pos(inode->i_sb, sbi->meta_blkaddr) +
345b780d3fcSGao Xiang 		(EROFS_I(inode)->nid << sbi->islotbits);
34647e4937aSGao Xiang }
34747e4937aSGao Xiang 
3488a765682SGao Xiang static inline unsigned int erofs_bitrange(unsigned int value, unsigned int bit,
3498a765682SGao Xiang 					  unsigned int bits)
35047e4937aSGao Xiang {
3518a765682SGao Xiang 
3528a765682SGao Xiang 	return (value >> bit) & ((1 << bits) - 1);
35347e4937aSGao Xiang }
35447e4937aSGao Xiang 
3558a765682SGao Xiang 
3568a765682SGao Xiang static inline unsigned int erofs_inode_version(unsigned int value)
35747e4937aSGao Xiang {
3588a765682SGao Xiang 	return erofs_bitrange(value, EROFS_I_VERSION_BIT,
3598a765682SGao Xiang 			      EROFS_I_VERSION_BITS);
3608a765682SGao Xiang }
3618a765682SGao Xiang 
3628a765682SGao Xiang static inline unsigned int erofs_inode_datalayout(unsigned int value)
3638a765682SGao Xiang {
3648a765682SGao Xiang 	return erofs_bitrange(value, EROFS_I_DATALAYOUT_BIT,
3658a765682SGao Xiang 			      EROFS_I_DATALAYOUT_BITS);
36647e4937aSGao Xiang }
36747e4937aSGao Xiang 
36838629291SGao Xiang /*
36938629291SGao Xiang  * Different from grab_cache_page_nowait(), reclaiming is never triggered
37038629291SGao Xiang  * when allocating new pages.
37138629291SGao Xiang  */
37238629291SGao Xiang static inline
37338629291SGao Xiang struct page *erofs_grab_cache_page_nowait(struct address_space *mapping,
37438629291SGao Xiang 					  pgoff_t index)
37538629291SGao Xiang {
37638629291SGao Xiang 	return pagecache_get_page(mapping, index,
37738629291SGao Xiang 			FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
37838629291SGao Xiang 			readahead_gfp_mask(mapping) & ~__GFP_RECLAIM);
37938629291SGao Xiang }
38038629291SGao Xiang 
38147e4937aSGao Xiang /* Has a disk mapping */
382768bb10aSGao Xiang #define EROFS_MAP_MAPPED	0x0001
38347e4937aSGao Xiang /* Located in metadata (could be copied from bd_inode) */
384768bb10aSGao Xiang #define EROFS_MAP_META		0x0002
3858f899262SGao Xiang /* The extent is encoded */
386768bb10aSGao Xiang #define EROFS_MAP_ENCODED	0x0004
38747e4937aSGao Xiang /* The length of extent is full */
388768bb10aSGao Xiang #define EROFS_MAP_FULL_MAPPED	0x0008
389b15b2e30SYue Hu /* Located in the special packed inode */
390768bb10aSGao Xiang #define EROFS_MAP_FRAGMENT	0x0010
3915c2a6425SGao Xiang /* The extent refers to partial decompressed data */
392768bb10aSGao Xiang #define EROFS_MAP_PARTIAL_REF	0x0020
39347e4937aSGao Xiang 
39447e4937aSGao Xiang struct erofs_map_blocks {
39509c54379SGao Xiang 	struct erofs_buf buf;
39609c54379SGao Xiang 
39747e4937aSGao Xiang 	erofs_off_t m_pa, m_la;
39847e4937aSGao Xiang 	u64 m_plen, m_llen;
39947e4937aSGao Xiang 
400dfeab2e9SGao Xiang 	unsigned short m_deviceid;
4018f899262SGao Xiang 	char m_algorithmformat;
40247e4937aSGao Xiang 	unsigned int m_flags;
40347e4937aSGao Xiang };
40447e4937aSGao Xiang 
405d95ae5e2SGao Xiang /*
406d95ae5e2SGao Xiang  * Used to get the exact decompressed length, e.g. fiemap (consider lookback
407d95ae5e2SGao Xiang  * approach instead if possible since it's more metadata lightweight.)
408d95ae5e2SGao Xiang  */
4098b58f9f0SJingbo Xu #define EROFS_GET_BLOCKS_FIEMAP		0x0001
410622ceaddSGao Xiang /* Used to map the whole extent if non-negligible data is requested for LZMA */
4118b58f9f0SJingbo Xu #define EROFS_GET_BLOCKS_READMORE	0x0002
412b15b2e30SYue Hu /* Used to map tail extent for tailpacking inline or fragment pcluster */
4138b58f9f0SJingbo Xu #define EROFS_GET_BLOCKS_FINDTAIL	0x0004
41447e4937aSGao Xiang 
4158f899262SGao Xiang enum {
4168f899262SGao Xiang 	Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
417fdffc091SYue Hu 	Z_EROFS_COMPRESSION_INTERLACED,
4188f899262SGao Xiang 	Z_EROFS_COMPRESSION_RUNTIME_MAX
4198f899262SGao Xiang };
4208f899262SGao Xiang 
421dfeab2e9SGao Xiang struct erofs_map_dev {
422955b478eSJeffle Xu 	struct erofs_fscache *m_fscache;
423dfeab2e9SGao Xiang 	struct block_device *m_bdev;
424dfeab2e9SGao Xiang 	struct dax_device *m_daxdev;
425de205114SChristoph Hellwig 	u64 m_dax_part_off;
426dfeab2e9SGao Xiang 
427dfeab2e9SGao Xiang 	erofs_off_t m_pa;
428dfeab2e9SGao Xiang 	unsigned int m_deviceid;
429dfeab2e9SGao Xiang };
430dfeab2e9SGao Xiang 
431557afdd9SGao Xiang extern struct file_system_type erofs_fs_type;
432557afdd9SGao Xiang extern const struct super_operations erofs_sops;
433557afdd9SGao Xiang 
434557afdd9SGao Xiang extern const struct address_space_operations erofs_raw_access_aops;
435557afdd9SGao Xiang extern const struct address_space_operations z_erofs_aops;
436557afdd9SGao Xiang extern const struct address_space_operations erofs_fscache_access_aops;
437557afdd9SGao Xiang 
438557afdd9SGao Xiang extern const struct inode_operations erofs_generic_iops;
439557afdd9SGao Xiang extern const struct inode_operations erofs_symlink_iops;
440557afdd9SGao Xiang extern const struct inode_operations erofs_fast_symlink_iops;
441557afdd9SGao Xiang extern const struct inode_operations erofs_dir_iops;
442557afdd9SGao Xiang 
443a08e67a0SHuang Jianan extern const struct file_operations erofs_file_fops;
444557afdd9SGao Xiang extern const struct file_operations erofs_dir_fops;
445557afdd9SGao Xiang 
446557afdd9SGao Xiang extern const struct iomap_ops z_erofs_iomap_report_ops;
447557afdd9SGao Xiang 
448557afdd9SGao Xiang /* flags for erofs_fscache_register_cookie() */
44961fef989SJingbo Xu #define EROFS_REG_COOKIE_SHARE		0x0001
45061fef989SJingbo Xu #define EROFS_REG_COOKIE_NEED_NOEXIST	0x0002
451557afdd9SGao Xiang 
4529e382914SJingbo Xu void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
4539e382914SJingbo Xu 			  erofs_off_t *offset, int *lengthp);
45409c54379SGao Xiang void erofs_unmap_metabuf(struct erofs_buf *buf);
455c521e3adSGao Xiang void erofs_put_metabuf(struct erofs_buf *buf);
456eb2c5e41SGao Xiang void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
457eb2c5e41SGao Xiang 		  enum erofs_kmap_type type);
458eb2c5e41SGao Xiang void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
459c521e3adSGao Xiang void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
460c521e3adSGao Xiang 			 erofs_blk_t blkaddr, enum erofs_kmap_type type);
461dfeab2e9SGao Xiang int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
462eadcd6b5SGao Xiang int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
463eadcd6b5SGao Xiang 		 u64 start, u64 len);
4648b58f9f0SJingbo Xu int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
465312fe643SGao Xiang struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
466b74d24f7SChristian Brauner int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
467549c7297SChristian Brauner 		  struct kstat *stat, u32 request_mask,
468549c7297SChristian Brauner 		  unsigned int query_flags);
4693e917cc3SHongnan Li int erofs_namei(struct inode *dir, const struct qstr *name,
47047e4937aSGao Xiang 		erofs_nid_t *nid, unsigned int *d_type);
47147e4937aSGao Xiang 
472598162d0SGao Xiang static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
473598162d0SGao Xiang {
474598162d0SGao Xiang 	int retried = 0;
475598162d0SGao Xiang 
476598162d0SGao Xiang 	while (1) {
477598162d0SGao Xiang 		void *p = vm_map_ram(pages, count, -1);
478598162d0SGao Xiang 
479598162d0SGao Xiang 		/* retry two more times (totally 3 times) */
480598162d0SGao Xiang 		if (p || ++retried >= 3)
481598162d0SGao Xiang 			return p;
482598162d0SGao Xiang 		vm_unmap_aliases();
483598162d0SGao Xiang 	}
484598162d0SGao Xiang 	return NULL;
485598162d0SGao Xiang }
486598162d0SGao Xiang 
48752488734SGao Xiang void *erofs_get_pcpubuf(unsigned int requiredpages);
48852488734SGao Xiang void erofs_put_pcpubuf(void *ptr);
48952488734SGao Xiang int erofs_pcpubuf_growsize(unsigned int nrpages);
490a279adedSYangtao Li void __init erofs_pcpubuf_init(void);
49152488734SGao Xiang void erofs_pcpubuf_exit(void);
49252488734SGao Xiang 
493168e9a76SHuang Jianan int erofs_register_sysfs(struct super_block *sb);
494168e9a76SHuang Jianan void erofs_unregister_sysfs(struct super_block *sb);
495168e9a76SHuang Jianan int __init erofs_init_sysfs(void);
496168e9a76SHuang Jianan void erofs_exit_sysfs(void);
497168e9a76SHuang Jianan 
498eaa9172aSGao Xiang struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
499557afdd9SGao Xiang static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
500eaa9172aSGao Xiang {
501eaa9172aSGao Xiang 	set_page_private(page, (unsigned long)*pagepool);
502eaa9172aSGao Xiang 	*pagepool = page;
503eaa9172aSGao Xiang }
504eaa9172aSGao Xiang void erofs_release_pages(struct page **pagepool);
50547e4937aSGao Xiang 
50647e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
50747e4937aSGao Xiang int erofs_workgroup_put(struct erofs_workgroup *grp);
50847e4937aSGao Xiang struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
509997626d8SVladimir Zapolskiy 					     pgoff_t index);
51064094a04SGao Xiang struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
511e5e9a432SVladimir Zapolskiy 					       struct erofs_workgroup *grp);
51247e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
51347e4937aSGao Xiang void erofs_shrinker_register(struct super_block *sb);
51447e4937aSGao Xiang void erofs_shrinker_unregister(struct super_block *sb);
51547e4937aSGao Xiang int __init erofs_init_shrinker(void);
51647e4937aSGao Xiang void erofs_exit_shrinker(void);
51747e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void);
51847e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void);
51947e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
52047e4937aSGao Xiang 				       struct erofs_workgroup *egrp);
521d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page);
5225d50538fSHuang Jianan int z_erofs_load_lz4_config(struct super_block *sb,
52346249cdeSGao Xiang 			    struct erofs_super_block *dsb,
52446249cdeSGao Xiang 			    struct z_erofs_lz4_cfgs *lz4, int len);
525557afdd9SGao Xiang int z_erofs_fill_inode(struct inode *inode);
526557afdd9SGao Xiang int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
527557afdd9SGao Xiang 			    int flags);
52847e4937aSGao Xiang #else
52947e4937aSGao Xiang static inline void erofs_shrinker_register(struct super_block *sb) {}
53047e4937aSGao Xiang static inline void erofs_shrinker_unregister(struct super_block *sb) {}
53147e4937aSGao Xiang static inline int erofs_init_shrinker(void) { return 0; }
53247e4937aSGao Xiang static inline void erofs_exit_shrinker(void) {}
53347e4937aSGao Xiang static inline int z_erofs_init_zip_subsystem(void) { return 0; }
53447e4937aSGao Xiang static inline void z_erofs_exit_zip_subsystem(void) {}
5355d50538fSHuang Jianan static inline int z_erofs_load_lz4_config(struct super_block *sb,
53646249cdeSGao Xiang 				  struct erofs_super_block *dsb,
53746249cdeSGao Xiang 				  struct z_erofs_lz4_cfgs *lz4, int len)
5385d50538fSHuang Jianan {
53914373711SGao Xiang 	if (lz4 || dsb->u1.lz4_max_distance) {
5405d50538fSHuang Jianan 		erofs_err(sb, "lz4 algorithm isn't enabled");
5415d50538fSHuang Jianan 		return -EINVAL;
5425d50538fSHuang Jianan 	}
5435d50538fSHuang Jianan 	return 0;
5445d50538fSHuang Jianan }
545557afdd9SGao Xiang static inline int z_erofs_fill_inode(struct inode *inode) { return -EOPNOTSUPP; }
54647e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_ZIP */
54747e4937aSGao Xiang 
548622ceaddSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP_LZMA
549a279adedSYangtao Li int __init z_erofs_lzma_init(void);
550622ceaddSGao Xiang void z_erofs_lzma_exit(void);
551622ceaddSGao Xiang int z_erofs_load_lzma_config(struct super_block *sb,
552622ceaddSGao Xiang 			     struct erofs_super_block *dsb,
553622ceaddSGao Xiang 			     struct z_erofs_lzma_cfgs *lzma, int size);
554622ceaddSGao Xiang #else
555622ceaddSGao Xiang static inline int z_erofs_lzma_init(void) { return 0; }
556622ceaddSGao Xiang static inline int z_erofs_lzma_exit(void) { return 0; }
557622ceaddSGao Xiang static inline int z_erofs_load_lzma_config(struct super_block *sb,
558622ceaddSGao Xiang 			     struct erofs_super_block *dsb,
559622ceaddSGao Xiang 			     struct z_erofs_lzma_cfgs *lzma, int size) {
560622ceaddSGao Xiang 	if (lzma) {
561622ceaddSGao Xiang 		erofs_err(sb, "lzma algorithm isn't enabled");
562622ceaddSGao Xiang 		return -EINVAL;
563622ceaddSGao Xiang 	}
564622ceaddSGao Xiang 	return 0;
565622ceaddSGao Xiang }
566557afdd9SGao Xiang #endif	/* !CONFIG_EROFS_FS_ZIP_LZMA */
567622ceaddSGao Xiang 
568c6be2bd0SJeffle Xu #ifdef CONFIG_EROFS_FS_ONDEMAND
569c6be2bd0SJeffle Xu int erofs_fscache_register_fs(struct super_block *sb);
570c6be2bd0SJeffle Xu void erofs_fscache_unregister_fs(struct super_block *sb);
571b02c602fSJeffle Xu 
572e1de2da0SJia Zhu struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
573557afdd9SGao Xiang 					char *name, unsigned int flags);
574e1de2da0SJia Zhu void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
575c6be2bd0SJeffle Xu #else
576c6be2bd0SJeffle Xu static inline int erofs_fscache_register_fs(struct super_block *sb)
577c6be2bd0SJeffle Xu {
578e1de2da0SJia Zhu 	return -EOPNOTSUPP;
579c6be2bd0SJeffle Xu }
580c6be2bd0SJeffle Xu static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
581b02c602fSJeffle Xu 
582e1de2da0SJia Zhu static inline
583e1de2da0SJia Zhu struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
584557afdd9SGao Xiang 					char *name, unsigned int flags)
585b02c602fSJeffle Xu {
586e1de2da0SJia Zhu 	return ERR_PTR(-EOPNOTSUPP);
587b02c602fSJeffle Xu }
588b02c602fSJeffle Xu 
589e1de2da0SJia Zhu static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
590b02c602fSJeffle Xu {
591b02c602fSJeffle Xu }
592c6be2bd0SJeffle Xu #endif
593c6be2bd0SJeffle Xu 
59447e4937aSGao Xiang #define EFSCORRUPTED    EUCLEAN         /* Filesystem is corrupted */
59547e4937aSGao Xiang 
59647e4937aSGao Xiang #endif	/* __EROFS_INTERNAL_H */
597