xref: /openbmc/linux/fs/erofs/internal.h (revision 1442b02b)
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/buffer_head.h>
1647e4937aSGao Xiang #include <linux/magic.h>
1747e4937aSGao Xiang #include <linux/slab.h>
1847e4937aSGao Xiang #include <linux/vmalloc.h>
19eadcd6b5SGao Xiang #include <linux/iomap.h>
2047e4937aSGao Xiang #include "erofs_fs.h"
2147e4937aSGao Xiang 
2247e4937aSGao Xiang /* redefine pr_fmt "erofs: " */
2347e4937aSGao Xiang #undef pr_fmt
2447e4937aSGao Xiang #define pr_fmt(fmt) "erofs: " fmt
2547e4937aSGao Xiang 
264f761fa2SGao Xiang __printf(3, 4) void _erofs_err(struct super_block *sb,
274f761fa2SGao Xiang 			       const char *function, const char *fmt, ...);
284f761fa2SGao Xiang #define erofs_err(sb, fmt, ...)	\
294f761fa2SGao Xiang 	_erofs_err(sb, __func__, fmt "\n", ##__VA_ARGS__)
304f761fa2SGao Xiang __printf(3, 4) void _erofs_info(struct super_block *sb,
314f761fa2SGao Xiang 			       const char *function, const char *fmt, ...);
324f761fa2SGao Xiang #define erofs_info(sb, fmt, ...) \
334f761fa2SGao Xiang 	_erofs_info(sb, __func__, fmt "\n", ##__VA_ARGS__)
3447e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_DEBUG
354f761fa2SGao Xiang #define erofs_dbg(x, ...)       pr_debug(x "\n", ##__VA_ARGS__)
3647e4937aSGao Xiang #define DBG_BUGON               BUG_ON
3747e4937aSGao Xiang #else
384f761fa2SGao Xiang #define erofs_dbg(x, ...)       ((void)0)
3947e4937aSGao Xiang #define DBG_BUGON(x)            ((void)(x))
4047e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_DEBUG */
4147e4937aSGao Xiang 
4247e4937aSGao Xiang /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
4347e4937aSGao Xiang #define EROFS_SUPER_MAGIC   EROFS_SUPER_MAGIC_V1
4447e4937aSGao Xiang 
4547e4937aSGao Xiang typedef u64 erofs_nid_t;
4647e4937aSGao Xiang typedef u64 erofs_off_t;
4747e4937aSGao Xiang /* data type for filesystem-wide blocks number */
4847e4937aSGao Xiang typedef u32 erofs_blk_t;
4947e4937aSGao Xiang 
50dfeab2e9SGao Xiang struct erofs_device_info {
51dfeab2e9SGao Xiang 	char *path;
52955b478eSJeffle Xu 	struct erofs_fscache *fscache;
53dfeab2e9SGao Xiang 	struct block_device *bdev;
54dfeab2e9SGao Xiang 	struct dax_device *dax_dev;
55cd913c76SChristoph Hellwig 	u64 dax_part_off;
56dfeab2e9SGao Xiang 
57dfeab2e9SGao Xiang 	u32 blocks;
58dfeab2e9SGao Xiang 	u32 mapped_blkaddr;
59dfeab2e9SGao Xiang };
60dfeab2e9SGao Xiang 
6140452ffcSHuang Jianan enum {
6240452ffcSHuang Jianan 	EROFS_SYNC_DECOMPRESS_AUTO,
6340452ffcSHuang Jianan 	EROFS_SYNC_DECOMPRESS_FORCE_ON,
6440452ffcSHuang Jianan 	EROFS_SYNC_DECOMPRESS_FORCE_OFF
6540452ffcSHuang Jianan };
6640452ffcSHuang Jianan 
67e6242465SGao Xiang struct erofs_mount_opts {
68f57a3fe4SChao Yu #ifdef CONFIG_EROFS_FS_ZIP
69f57a3fe4SChao Yu 	/* current strategy of how to use managed cache */
70f57a3fe4SChao Yu 	unsigned char cache_strategy;
7140452ffcSHuang Jianan 	/* strategy of sync decompression (0 - auto, 1 - force on, 2 - force off) */
7240452ffcSHuang Jianan 	unsigned int sync_decompress;
73f57a3fe4SChao Yu 
74f57a3fe4SChao Yu 	/* threshold for decompression synchronously */
75f57a3fe4SChao Yu 	unsigned int max_sync_decompress_pages;
76f57a3fe4SChao Yu #endif
77f57a3fe4SChao Yu 	unsigned int mount_opt;
78c6be2bd0SJeffle Xu 	char *fsid;
79f57a3fe4SChao Yu };
80f57a3fe4SChao Yu 
81dfeab2e9SGao Xiang struct erofs_dev_context {
82dfeab2e9SGao Xiang 	struct idr tree;
83dfeab2e9SGao Xiang 	struct rw_semaphore rwsem;
84dfeab2e9SGao Xiang 
85dfeab2e9SGao Xiang 	unsigned int extra_devices;
86dfeab2e9SGao Xiang };
87dfeab2e9SGao Xiang 
88e6242465SGao Xiang struct erofs_fs_context {
89e6242465SGao Xiang 	struct erofs_mount_opts opt;
90dfeab2e9SGao Xiang 	struct erofs_dev_context *devs;
91e6242465SGao Xiang };
92e6242465SGao Xiang 
935d50538fSHuang Jianan /* all filesystem-wide lz4 configurations */
945d50538fSHuang Jianan struct erofs_sb_lz4_info {
955d50538fSHuang Jianan 	/* # of pages needed for EROFS lz4 rolling decompression */
965d50538fSHuang Jianan 	u16 max_distance_pages;
974fea63f7SGao Xiang 	/* maximum possible blocks for pclusters in the filesystem */
984fea63f7SGao Xiang 	u16 max_pclusterblks;
995d50538fSHuang Jianan };
1005d50538fSHuang Jianan 
101b02c602fSJeffle Xu struct erofs_fscache {
102b02c602fSJeffle Xu 	struct fscache_cookie *cookie;
1033c265d7dSJeffle Xu 	struct inode *inode;
104b02c602fSJeffle Xu };
105b02c602fSJeffle Xu 
10647e4937aSGao Xiang struct erofs_sb_info {
107e6242465SGao Xiang 	struct erofs_mount_opts opt;	/* options */
10847e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
10947e4937aSGao Xiang 	/* list for all registered superblocks, mainly for shrinker */
11047e4937aSGao Xiang 	struct list_head list;
11147e4937aSGao Xiang 	struct mutex umount_mutex;
11247e4937aSGao Xiang 
11364094a04SGao Xiang 	/* managed XArray arranged in physical block number */
11464094a04SGao Xiang 	struct xarray managed_pslots;
11547e4937aSGao Xiang 
11647e4937aSGao Xiang 	unsigned int shrinker_run_no;
11714373711SGao Xiang 	u16 available_compr_algs;
11847e4937aSGao Xiang 
11947e4937aSGao Xiang 	/* pseudo inode to manage cached pages */
12047e4937aSGao Xiang 	struct inode *managed_cache;
1215d50538fSHuang Jianan 
1225d50538fSHuang Jianan 	struct erofs_sb_lz4_info lz4;
12347e4937aSGao Xiang #endif	/* CONFIG_EROFS_FS_ZIP */
124dfeab2e9SGao Xiang 	struct erofs_dev_context *devs;
12506252e9cSGao Xiang 	struct dax_device *dax_dev;
126cd913c76SChristoph Hellwig 	u64 dax_part_off;
127dfeab2e9SGao Xiang 	u64 total_blocks;
128dfeab2e9SGao Xiang 	u32 primarydevice_blocks;
129dfeab2e9SGao Xiang 
13047e4937aSGao Xiang 	u32 meta_blkaddr;
13147e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_XATTR
13247e4937aSGao Xiang 	u32 xattr_blkaddr;
13347e4937aSGao Xiang #endif
134dfeab2e9SGao Xiang 	u16 device_id_mask;	/* valid bits of device id to be used */
13547e4937aSGao Xiang 
13647e4937aSGao Xiang 	/* inode slot unit size in bit shift */
13747e4937aSGao Xiang 	unsigned char islotbits;
13847e4937aSGao Xiang 
13914373711SGao Xiang 	u32 sb_size;			/* total superblock size */
14047e4937aSGao Xiang 	u32 build_time_nsec;
14147e4937aSGao Xiang 	u64 build_time;
14247e4937aSGao Xiang 
14347e4937aSGao Xiang 	/* what we really care is nid, rather than ino.. */
14447e4937aSGao Xiang 	erofs_nid_t root_nid;
14547e4937aSGao Xiang 	/* used for statfs, f_files - f_favail */
14647e4937aSGao Xiang 	u64 inos;
14747e4937aSGao Xiang 
14847e4937aSGao Xiang 	u8 uuid[16];                    /* 128-bit uuid for volume */
14947e4937aSGao Xiang 	u8 volume_name[16];             /* volume name */
150b858a484SPratik Shinde 	u32 feature_compat;
151426a9308SGao Xiang 	u32 feature_incompat;
152168e9a76SHuang Jianan 
153168e9a76SHuang Jianan 	/* sysfs support */
154168e9a76SHuang Jianan 	struct kobject s_kobj;		/* /sys/fs/erofs/<devname> */
155168e9a76SHuang Jianan 	struct completion s_kobj_unregister;
156c6be2bd0SJeffle Xu 
157c6be2bd0SJeffle Xu 	/* fscache support */
158c6be2bd0SJeffle Xu 	struct fscache_volume *volume;
15937c90c5fSJeffle Xu 	struct erofs_fscache *s_fscache;
16047e4937aSGao Xiang };
16147e4937aSGao Xiang 
16247e4937aSGao Xiang #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
16347e4937aSGao Xiang #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
16447e4937aSGao Xiang 
16547e4937aSGao Xiang /* Mount flags set via mount options or defaults */
16647e4937aSGao Xiang #define EROFS_MOUNT_XATTR_USER		0x00000010
16747e4937aSGao Xiang #define EROFS_MOUNT_POSIX_ACL		0x00000020
16806252e9cSGao Xiang #define EROFS_MOUNT_DAX_ALWAYS		0x00000040
16906252e9cSGao Xiang #define EROFS_MOUNT_DAX_NEVER		0x00000080
17047e4937aSGao Xiang 
171e6242465SGao Xiang #define clear_opt(opt, option)	((opt)->mount_opt &= ~EROFS_MOUNT_##option)
172e6242465SGao Xiang #define set_opt(opt, option)	((opt)->mount_opt |= EROFS_MOUNT_##option)
173e6242465SGao Xiang #define test_opt(opt, option)	((opt)->mount_opt & EROFS_MOUNT_##option)
17447e4937aSGao Xiang 
17593b856bbSJeffle Xu static inline bool erofs_is_fscache_mode(struct super_block *sb)
17693b856bbSJeffle Xu {
17793b856bbSJeffle Xu 	return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && !sb->s_bdev;
17893b856bbSJeffle Xu }
17993b856bbSJeffle Xu 
18047e4937aSGao Xiang enum {
18147e4937aSGao Xiang 	EROFS_ZIP_CACHE_DISABLED,
18247e4937aSGao Xiang 	EROFS_ZIP_CACHE_READAHEAD,
18347e4937aSGao Xiang 	EROFS_ZIP_CACHE_READAROUND
18447e4937aSGao Xiang };
18547e4937aSGao Xiang 
186f57a3fe4SChao Yu #ifdef CONFIG_EROFS_FS_ZIP
18747e4937aSGao Xiang #define EROFS_LOCKED_MAGIC     (INT_MIN | 0xE0F510CCL)
18847e4937aSGao Xiang 
18947e4937aSGao Xiang /* basic unit of the workstation of a super_block */
19047e4937aSGao Xiang struct erofs_workgroup {
19147e4937aSGao Xiang 	/* the workgroup index in the workstation */
19247e4937aSGao Xiang 	pgoff_t index;
19347e4937aSGao Xiang 
19447e4937aSGao Xiang 	/* overall workgroup reference count */
19547e4937aSGao Xiang 	atomic_t refcount;
19647e4937aSGao Xiang };
19747e4937aSGao Xiang 
19847e4937aSGao Xiang #if defined(CONFIG_SMP)
19947e4937aSGao Xiang static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
20047e4937aSGao Xiang 						 int val)
20147e4937aSGao Xiang {
20247e4937aSGao Xiang 	preempt_disable();
20347e4937aSGao Xiang 	if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
20447e4937aSGao Xiang 		preempt_enable();
20547e4937aSGao Xiang 		return false;
20647e4937aSGao Xiang 	}
20747e4937aSGao Xiang 	return true;
20847e4937aSGao Xiang }
20947e4937aSGao Xiang 
21047e4937aSGao Xiang static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
21147e4937aSGao Xiang 					    int orig_val)
21247e4937aSGao Xiang {
21347e4937aSGao Xiang 	/*
21447e4937aSGao Xiang 	 * other observers should notice all modifications
21547e4937aSGao Xiang 	 * in the freezing period.
21647e4937aSGao Xiang 	 */
21747e4937aSGao Xiang 	smp_mb();
21847e4937aSGao Xiang 	atomic_set(&grp->refcount, orig_val);
21947e4937aSGao Xiang 	preempt_enable();
22047e4937aSGao Xiang }
22147e4937aSGao Xiang 
22247e4937aSGao Xiang static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
22347e4937aSGao Xiang {
22447e4937aSGao Xiang 	return atomic_cond_read_relaxed(&grp->refcount,
22547e4937aSGao Xiang 					VAL != EROFS_LOCKED_MAGIC);
22647e4937aSGao Xiang }
22747e4937aSGao Xiang #else
22847e4937aSGao Xiang static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
22947e4937aSGao Xiang 						 int val)
23047e4937aSGao Xiang {
23147e4937aSGao Xiang 	preempt_disable();
23247e4937aSGao Xiang 	/* no need to spin on UP platforms, let's just disable preemption. */
23347e4937aSGao Xiang 	if (val != atomic_read(&grp->refcount)) {
23447e4937aSGao Xiang 		preempt_enable();
23547e4937aSGao Xiang 		return false;
23647e4937aSGao Xiang 	}
23747e4937aSGao Xiang 	return true;
23847e4937aSGao Xiang }
23947e4937aSGao Xiang 
24047e4937aSGao Xiang static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
24147e4937aSGao Xiang 					    int orig_val)
24247e4937aSGao Xiang {
24347e4937aSGao Xiang 	preempt_enable();
24447e4937aSGao Xiang }
24547e4937aSGao Xiang 
24647e4937aSGao Xiang static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
24747e4937aSGao Xiang {
24847e4937aSGao Xiang 	int v = atomic_read(&grp->refcount);
24947e4937aSGao Xiang 
25047e4937aSGao Xiang 	/* workgroup is never freezed on uniprocessor systems */
25147e4937aSGao Xiang 	DBG_BUGON(v == EROFS_LOCKED_MAGIC);
25247e4937aSGao Xiang 	return v;
25347e4937aSGao Xiang }
25447e4937aSGao Xiang #endif	/* !CONFIG_SMP */
25547e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_ZIP */
25647e4937aSGao Xiang 
25747e4937aSGao Xiang /* we strictly follow PAGE_SIZE and no buffer head yet */
25847e4937aSGao Xiang #define LOG_BLOCK_SIZE		PAGE_SHIFT
25947e4937aSGao Xiang 
26047e4937aSGao Xiang #undef LOG_SECTORS_PER_BLOCK
26147e4937aSGao Xiang #define LOG_SECTORS_PER_BLOCK	(PAGE_SHIFT - 9)
26247e4937aSGao Xiang 
26347e4937aSGao Xiang #undef SECTORS_PER_BLOCK
26447e4937aSGao Xiang #define SECTORS_PER_BLOCK	(1 << SECTORS_PER_BLOCK)
26547e4937aSGao Xiang 
26647e4937aSGao Xiang #define EROFS_BLKSIZ		(1 << LOG_BLOCK_SIZE)
26747e4937aSGao Xiang 
26847e4937aSGao Xiang #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
26947e4937aSGao Xiang #error erofs cannot be used in this platform
27047e4937aSGao Xiang #endif
27147e4937aSGao Xiang 
272fdf80a47SGao Xiang enum erofs_kmap_type {
273fdf80a47SGao Xiang 	EROFS_NO_KMAP,		/* don't map the buffer */
274fdf80a47SGao Xiang 	EROFS_KMAP,		/* use kmap() to map the buffer */
275fdf80a47SGao Xiang 	EROFS_KMAP_ATOMIC,	/* use kmap_atomic() to map the buffer */
276fdf80a47SGao Xiang };
277fdf80a47SGao Xiang 
278fdf80a47SGao Xiang struct erofs_buf {
279fdf80a47SGao Xiang 	struct page *page;
280fdf80a47SGao Xiang 	void *base;
281fdf80a47SGao Xiang 	enum erofs_kmap_type kmap_type;
282fdf80a47SGao Xiang };
283fdf80a47SGao Xiang #define __EROFS_BUF_INITIALIZER	((struct erofs_buf){ .page = NULL })
284fdf80a47SGao Xiang 
28547e4937aSGao Xiang #define ROOT_NID(sb)		((sb)->root_nid)
28647e4937aSGao Xiang 
28747e4937aSGao Xiang #define erofs_blknr(addr)       ((addr) / EROFS_BLKSIZ)
28847e4937aSGao Xiang #define erofs_blkoff(addr)      ((addr) % EROFS_BLKSIZ)
28947e4937aSGao Xiang #define blknr_to_addr(nr)       ((erofs_off_t)(nr) * EROFS_BLKSIZ)
29047e4937aSGao Xiang 
29147e4937aSGao Xiang static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
29247e4937aSGao Xiang {
29347e4937aSGao Xiang 	return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
29447e4937aSGao Xiang }
29547e4937aSGao Xiang 
296de06a6a3SGao Xiang #define EROFS_FEATURE_FUNCS(name, compat, feature) \
297de06a6a3SGao Xiang static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
298de06a6a3SGao Xiang { \
299de06a6a3SGao Xiang 	return sbi->feature_##compat & EROFS_FEATURE_##feature; \
300de06a6a3SGao Xiang }
301de06a6a3SGao Xiang 
3027e508f2cSHuang Jianan EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
30314373711SGao Xiang EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
3045404c330SGao Xiang EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
305168e9a76SHuang Jianan EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
306dfeab2e9SGao Xiang EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
307168e9a76SHuang Jianan EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
308ab92184fSYue Hu EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
309de06a6a3SGao Xiang EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
310de06a6a3SGao Xiang 
31147e4937aSGao Xiang /* atomic flag definitions */
312a5876e24SGao Xiang #define EROFS_I_EA_INITED_BIT	0
313a5876e24SGao Xiang #define EROFS_I_Z_INITED_BIT	1
31447e4937aSGao Xiang 
31547e4937aSGao Xiang /* bitlock definitions (arranged in reverse order) */
316a5876e24SGao Xiang #define EROFS_I_BL_XATTR_BIT	(BITS_PER_LONG - 1)
317a5876e24SGao Xiang #define EROFS_I_BL_Z_BIT	(BITS_PER_LONG - 2)
31847e4937aSGao Xiang 
319a5876e24SGao Xiang struct erofs_inode {
32047e4937aSGao Xiang 	erofs_nid_t nid;
32147e4937aSGao Xiang 
32247e4937aSGao Xiang 	/* atomic flags (including bitlocks) */
32347e4937aSGao Xiang 	unsigned long flags;
32447e4937aSGao Xiang 
3258a765682SGao Xiang 	unsigned char datalayout;
32647e4937aSGao Xiang 	unsigned char inode_isize;
32747e4937aSGao Xiang 	unsigned short xattr_isize;
32847e4937aSGao Xiang 
32947e4937aSGao Xiang 	unsigned int xattr_shared_count;
33047e4937aSGao Xiang 	unsigned int *xattr_shared_xattrs;
33147e4937aSGao Xiang 
33247e4937aSGao Xiang 	union {
33347e4937aSGao Xiang 		erofs_blk_t raw_blkaddr;
334c5aa903aSGao Xiang 		struct {
335c5aa903aSGao Xiang 			unsigned short	chunkformat;
336c5aa903aSGao Xiang 			unsigned char	chunkbits;
337c5aa903aSGao Xiang 		};
33847e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
33947e4937aSGao Xiang 		struct {
34047e4937aSGao Xiang 			unsigned short z_advise;
34147e4937aSGao Xiang 			unsigned char  z_algorithmtype[2];
34247e4937aSGao Xiang 			unsigned char  z_logical_clusterbits;
343ab92184fSYue Hu 			unsigned long  z_tailextent_headlcn;
34422ba5e99SGao Xiang 			erofs_off_t    z_idataoff;
345ab92184fSYue Hu 			unsigned short z_idata_size;
34647e4937aSGao Xiang 		};
34747e4937aSGao Xiang #endif	/* CONFIG_EROFS_FS_ZIP */
34847e4937aSGao Xiang 	};
34947e4937aSGao Xiang 	/* the corresponding vfs inode */
35047e4937aSGao Xiang 	struct inode vfs_inode;
35147e4937aSGao Xiang };
35247e4937aSGao Xiang 
353a5876e24SGao Xiang #define EROFS_I(ptr)	\
354a5876e24SGao Xiang 	container_of(ptr, struct erofs_inode, vfs_inode)
35547e4937aSGao Xiang 
35699634bf3SGao Xiang static inline unsigned long erofs_inode_datablocks(struct inode *inode)
35747e4937aSGao Xiang {
35847e4937aSGao Xiang 	/* since i_size cannot be changed */
35947e4937aSGao Xiang 	return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
36047e4937aSGao Xiang }
36147e4937aSGao Xiang 
3628a765682SGao Xiang static inline unsigned int erofs_bitrange(unsigned int value, unsigned int bit,
3638a765682SGao Xiang 					  unsigned int bits)
36447e4937aSGao Xiang {
3658a765682SGao Xiang 
3668a765682SGao Xiang 	return (value >> bit) & ((1 << bits) - 1);
36747e4937aSGao Xiang }
36847e4937aSGao Xiang 
3698a765682SGao Xiang 
3708a765682SGao Xiang static inline unsigned int erofs_inode_version(unsigned int value)
37147e4937aSGao Xiang {
3728a765682SGao Xiang 	return erofs_bitrange(value, EROFS_I_VERSION_BIT,
3738a765682SGao Xiang 			      EROFS_I_VERSION_BITS);
3748a765682SGao Xiang }
3758a765682SGao Xiang 
3768a765682SGao Xiang static inline unsigned int erofs_inode_datalayout(unsigned int value)
3778a765682SGao Xiang {
3788a765682SGao Xiang 	return erofs_bitrange(value, EROFS_I_DATALAYOUT_BIT,
3798a765682SGao Xiang 			      EROFS_I_DATALAYOUT_BITS);
38047e4937aSGao Xiang }
38147e4937aSGao Xiang 
38238629291SGao Xiang /*
38338629291SGao Xiang  * Different from grab_cache_page_nowait(), reclaiming is never triggered
38438629291SGao Xiang  * when allocating new pages.
38538629291SGao Xiang  */
38638629291SGao Xiang static inline
38738629291SGao Xiang struct page *erofs_grab_cache_page_nowait(struct address_space *mapping,
38838629291SGao Xiang 					  pgoff_t index)
38938629291SGao Xiang {
39038629291SGao Xiang 	return pagecache_get_page(mapping, index,
39138629291SGao Xiang 			FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
39238629291SGao Xiang 			readahead_gfp_mask(mapping) & ~__GFP_RECLAIM);
39338629291SGao Xiang }
39438629291SGao Xiang 
39547e4937aSGao Xiang extern const struct super_operations erofs_sops;
39647e4937aSGao Xiang 
39747e4937aSGao Xiang extern const struct address_space_operations erofs_raw_access_aops;
3980c638f70SGao Xiang extern const struct address_space_operations z_erofs_aops;
39947e4937aSGao Xiang 
40047e4937aSGao Xiang enum {
4018f899262SGao Xiang 	BH_Encoded = BH_PrivateStart,
40247e4937aSGao Xiang 	BH_FullMapped,
40347e4937aSGao Xiang };
40447e4937aSGao Xiang 
40547e4937aSGao Xiang /* Has a disk mapping */
40647e4937aSGao Xiang #define EROFS_MAP_MAPPED	(1 << BH_Mapped)
40747e4937aSGao Xiang /* Located in metadata (could be copied from bd_inode) */
40847e4937aSGao Xiang #define EROFS_MAP_META		(1 << BH_Meta)
4098f899262SGao Xiang /* The extent is encoded */
4108f899262SGao Xiang #define EROFS_MAP_ENCODED	(1 << BH_Encoded)
41147e4937aSGao Xiang /* The length of extent is full */
41247e4937aSGao Xiang #define EROFS_MAP_FULL_MAPPED	(1 << BH_FullMapped)
41347e4937aSGao Xiang 
41447e4937aSGao Xiang struct erofs_map_blocks {
41509c54379SGao Xiang 	struct erofs_buf buf;
41609c54379SGao Xiang 
41747e4937aSGao Xiang 	erofs_off_t m_pa, m_la;
41847e4937aSGao Xiang 	u64 m_plen, m_llen;
41947e4937aSGao Xiang 
420dfeab2e9SGao Xiang 	unsigned short m_deviceid;
4218f899262SGao Xiang 	char m_algorithmformat;
42247e4937aSGao Xiang 	unsigned int m_flags;
42347e4937aSGao Xiang };
42447e4937aSGao Xiang 
4258137824eSYue Hu /* Flags used by erofs_map_blocks_flatmode() */
42647e4937aSGao Xiang #define EROFS_GET_BLOCKS_RAW    0x0001
427d95ae5e2SGao Xiang /*
428d95ae5e2SGao Xiang  * Used to get the exact decompressed length, e.g. fiemap (consider lookback
429d95ae5e2SGao Xiang  * approach instead if possible since it's more metadata lightweight.)
430d95ae5e2SGao Xiang  */
431d95ae5e2SGao Xiang #define EROFS_GET_BLOCKS_FIEMAP	0x0002
432622ceaddSGao Xiang /* Used to map the whole extent if non-negligible data is requested for LZMA */
433622ceaddSGao Xiang #define EROFS_GET_BLOCKS_READMORE	0x0004
434ab92184fSYue Hu /* Used to map tail extent for tailpacking inline pcluster */
435ab92184fSYue Hu #define EROFS_GET_BLOCKS_FINDTAIL	0x0008
43647e4937aSGao Xiang 
4378f899262SGao Xiang enum {
4388f899262SGao Xiang 	Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
4398f899262SGao Xiang 	Z_EROFS_COMPRESSION_RUNTIME_MAX
4408f899262SGao Xiang };
4418f899262SGao Xiang 
44247e4937aSGao Xiang /* zmap.c */
443eadcd6b5SGao Xiang extern const struct iomap_ops z_erofs_iomap_report_ops;
444eadcd6b5SGao Xiang 
44547e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
44647e4937aSGao Xiang int z_erofs_fill_inode(struct inode *inode);
44747e4937aSGao Xiang int z_erofs_map_blocks_iter(struct inode *inode,
44847e4937aSGao Xiang 			    struct erofs_map_blocks *map,
44947e4937aSGao Xiang 			    int flags);
45047e4937aSGao Xiang #else
45147e4937aSGao Xiang static inline int z_erofs_fill_inode(struct inode *inode) { return -EOPNOTSUPP; }
45247e4937aSGao Xiang static inline int z_erofs_map_blocks_iter(struct inode *inode,
45347e4937aSGao Xiang 					  struct erofs_map_blocks *map,
45447e4937aSGao Xiang 					  int flags)
45547e4937aSGao Xiang {
45647e4937aSGao Xiang 	return -EOPNOTSUPP;
45747e4937aSGao Xiang }
45847e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_ZIP */
45947e4937aSGao Xiang 
460dfeab2e9SGao Xiang struct erofs_map_dev {
461955b478eSJeffle Xu 	struct erofs_fscache *m_fscache;
462dfeab2e9SGao Xiang 	struct block_device *m_bdev;
463dfeab2e9SGao Xiang 	struct dax_device *m_daxdev;
464de205114SChristoph Hellwig 	u64 m_dax_part_off;
465dfeab2e9SGao Xiang 
466dfeab2e9SGao Xiang 	erofs_off_t m_pa;
467dfeab2e9SGao Xiang 	unsigned int m_deviceid;
468dfeab2e9SGao Xiang };
469dfeab2e9SGao Xiang 
47047e4937aSGao Xiang /* data.c */
471a08e67a0SHuang Jianan extern const struct file_operations erofs_file_fops;
47209c54379SGao Xiang void erofs_unmap_metabuf(struct erofs_buf *buf);
473c521e3adSGao Xiang void erofs_put_metabuf(struct erofs_buf *buf);
474fe5de585SGao Xiang void *erofs_bread(struct erofs_buf *buf, struct inode *inode,
475fe5de585SGao Xiang 		  erofs_blk_t blkaddr, enum erofs_kmap_type type);
476c521e3adSGao Xiang void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
477c521e3adSGao Xiang 			 erofs_blk_t blkaddr, enum erofs_kmap_type type);
478dfeab2e9SGao Xiang int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
479eadcd6b5SGao Xiang int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
480eadcd6b5SGao Xiang 		 u64 start, u64 len);
48194d78946SJeffle Xu int erofs_map_blocks(struct inode *inode,
48294d78946SJeffle Xu 		     struct erofs_map_blocks *map, int flags);
48347e4937aSGao Xiang 
48447e4937aSGao Xiang /* inode.c */
48547e4937aSGao Xiang static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
48647e4937aSGao Xiang {
48747e4937aSGao Xiang #if BITS_PER_LONG == 32
48847e4937aSGao Xiang 	return (nid >> 32) ^ (nid & 0xffffffff);
48947e4937aSGao Xiang #else
49047e4937aSGao Xiang 	return nid;
49147e4937aSGao Xiang #endif
49247e4937aSGao Xiang }
49347e4937aSGao Xiang 
49447e4937aSGao Xiang extern const struct inode_operations erofs_generic_iops;
49547e4937aSGao Xiang extern const struct inode_operations erofs_symlink_iops;
49647e4937aSGao Xiang extern const struct inode_operations erofs_fast_symlink_iops;
49747e4937aSGao Xiang 
49847e4937aSGao Xiang struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir);
499549c7297SChristian Brauner int erofs_getattr(struct user_namespace *mnt_userns, const struct path *path,
500549c7297SChristian Brauner 		  struct kstat *stat, u32 request_mask,
501549c7297SChristian Brauner 		  unsigned int query_flags);
50247e4937aSGao Xiang 
50347e4937aSGao Xiang /* namei.c */
50447e4937aSGao Xiang extern const struct inode_operations erofs_dir_iops;
50547e4937aSGao Xiang 
5063e917cc3SHongnan Li int erofs_namei(struct inode *dir, const struct qstr *name,
50747e4937aSGao Xiang 		erofs_nid_t *nid, unsigned int *d_type);
50847e4937aSGao Xiang 
50947e4937aSGao Xiang /* dir.c */
51047e4937aSGao Xiang extern const struct file_operations erofs_dir_fops;
51147e4937aSGao Xiang 
512598162d0SGao Xiang static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
513598162d0SGao Xiang {
514598162d0SGao Xiang 	int retried = 0;
515598162d0SGao Xiang 
516598162d0SGao Xiang 	while (1) {
517598162d0SGao Xiang 		void *p = vm_map_ram(pages, count, -1);
518598162d0SGao Xiang 
519598162d0SGao Xiang 		/* retry two more times (totally 3 times) */
520598162d0SGao Xiang 		if (p || ++retried >= 3)
521598162d0SGao Xiang 			return p;
522598162d0SGao Xiang 		vm_unmap_aliases();
523598162d0SGao Xiang 	}
524598162d0SGao Xiang 	return NULL;
525598162d0SGao Xiang }
526598162d0SGao Xiang 
52752488734SGao Xiang /* pcpubuf.c */
52852488734SGao Xiang void *erofs_get_pcpubuf(unsigned int requiredpages);
52952488734SGao Xiang void erofs_put_pcpubuf(void *ptr);
53052488734SGao Xiang int erofs_pcpubuf_growsize(unsigned int nrpages);
53152488734SGao Xiang void erofs_pcpubuf_init(void);
53252488734SGao Xiang void erofs_pcpubuf_exit(void);
53352488734SGao Xiang 
534168e9a76SHuang Jianan /* sysfs.c */
535168e9a76SHuang Jianan int erofs_register_sysfs(struct super_block *sb);
536168e9a76SHuang Jianan void erofs_unregister_sysfs(struct super_block *sb);
537168e9a76SHuang Jianan int __init erofs_init_sysfs(void);
538168e9a76SHuang Jianan void erofs_exit_sysfs(void);
539168e9a76SHuang Jianan 
54047e4937aSGao Xiang /* utils.c / zdata.c */
541eaa9172aSGao Xiang struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
542eaa9172aSGao Xiang static inline void erofs_pagepool_add(struct page **pagepool,
543eaa9172aSGao Xiang 		struct page *page)
544eaa9172aSGao Xiang {
545eaa9172aSGao Xiang 	set_page_private(page, (unsigned long)*pagepool);
546eaa9172aSGao Xiang 	*pagepool = page;
547eaa9172aSGao Xiang }
548eaa9172aSGao Xiang void erofs_release_pages(struct page **pagepool);
54947e4937aSGao Xiang 
55047e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
55147e4937aSGao Xiang int erofs_workgroup_put(struct erofs_workgroup *grp);
55247e4937aSGao Xiang struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
553997626d8SVladimir Zapolskiy 					     pgoff_t index);
55464094a04SGao Xiang struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
555e5e9a432SVladimir Zapolskiy 					       struct erofs_workgroup *grp);
55647e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
55747e4937aSGao Xiang void erofs_shrinker_register(struct super_block *sb);
55847e4937aSGao Xiang void erofs_shrinker_unregister(struct super_block *sb);
55947e4937aSGao Xiang int __init erofs_init_shrinker(void);
56047e4937aSGao Xiang void erofs_exit_shrinker(void);
56147e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void);
56247e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void);
56347e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
56447e4937aSGao Xiang 				       struct erofs_workgroup *egrp);
565d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page);
5665d50538fSHuang Jianan int z_erofs_load_lz4_config(struct super_block *sb,
56746249cdeSGao Xiang 			    struct erofs_super_block *dsb,
56846249cdeSGao Xiang 			    struct z_erofs_lz4_cfgs *lz4, int len);
56947e4937aSGao Xiang #else
57047e4937aSGao Xiang static inline void erofs_shrinker_register(struct super_block *sb) {}
57147e4937aSGao Xiang static inline void erofs_shrinker_unregister(struct super_block *sb) {}
57247e4937aSGao Xiang static inline int erofs_init_shrinker(void) { return 0; }
57347e4937aSGao Xiang static inline void erofs_exit_shrinker(void) {}
57447e4937aSGao Xiang static inline int z_erofs_init_zip_subsystem(void) { return 0; }
57547e4937aSGao Xiang static inline void z_erofs_exit_zip_subsystem(void) {}
5765d50538fSHuang Jianan static inline int z_erofs_load_lz4_config(struct super_block *sb,
57746249cdeSGao Xiang 				  struct erofs_super_block *dsb,
57846249cdeSGao Xiang 				  struct z_erofs_lz4_cfgs *lz4, int len)
5795d50538fSHuang Jianan {
58014373711SGao Xiang 	if (lz4 || dsb->u1.lz4_max_distance) {
5815d50538fSHuang Jianan 		erofs_err(sb, "lz4 algorithm isn't enabled");
5825d50538fSHuang Jianan 		return -EINVAL;
5835d50538fSHuang Jianan 	}
5845d50538fSHuang Jianan 	return 0;
5855d50538fSHuang Jianan }
58647e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_ZIP */
58747e4937aSGao Xiang 
588622ceaddSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP_LZMA
589622ceaddSGao Xiang int z_erofs_lzma_init(void);
590622ceaddSGao Xiang void z_erofs_lzma_exit(void);
591622ceaddSGao Xiang int z_erofs_load_lzma_config(struct super_block *sb,
592622ceaddSGao Xiang 			     struct erofs_super_block *dsb,
593622ceaddSGao Xiang 			     struct z_erofs_lzma_cfgs *lzma, int size);
594622ceaddSGao Xiang #else
595622ceaddSGao Xiang static inline int z_erofs_lzma_init(void) { return 0; }
596622ceaddSGao Xiang static inline int z_erofs_lzma_exit(void) { return 0; }
597622ceaddSGao Xiang static inline int z_erofs_load_lzma_config(struct super_block *sb,
598622ceaddSGao Xiang 			     struct erofs_super_block *dsb,
599622ceaddSGao Xiang 			     struct z_erofs_lzma_cfgs *lzma, int size) {
600622ceaddSGao Xiang 	if (lzma) {
601622ceaddSGao Xiang 		erofs_err(sb, "lzma algorithm isn't enabled");
602622ceaddSGao Xiang 		return -EINVAL;
603622ceaddSGao Xiang 	}
604622ceaddSGao Xiang 	return 0;
605622ceaddSGao Xiang }
606622ceaddSGao Xiang #endif	/* !CONFIG_EROFS_FS_ZIP */
607622ceaddSGao Xiang 
608c6be2bd0SJeffle Xu /* fscache.c */
609c6be2bd0SJeffle Xu #ifdef CONFIG_EROFS_FS_ONDEMAND
610c6be2bd0SJeffle Xu int erofs_fscache_register_fs(struct super_block *sb);
611c6be2bd0SJeffle Xu void erofs_fscache_unregister_fs(struct super_block *sb);
612b02c602fSJeffle Xu 
613b02c602fSJeffle Xu int erofs_fscache_register_cookie(struct super_block *sb,
6143c265d7dSJeffle Xu 				  struct erofs_fscache **fscache,
6153c265d7dSJeffle Xu 				  char *name, bool need_inode);
616b02c602fSJeffle Xu void erofs_fscache_unregister_cookie(struct erofs_fscache **fscache);
617*1442b02bSJeffle Xu 
618*1442b02bSJeffle Xu extern const struct address_space_operations erofs_fscache_access_aops;
619c6be2bd0SJeffle Xu #else
620c6be2bd0SJeffle Xu static inline int erofs_fscache_register_fs(struct super_block *sb)
621c6be2bd0SJeffle Xu {
622c6be2bd0SJeffle Xu 	return 0;
623c6be2bd0SJeffle Xu }
624c6be2bd0SJeffle Xu static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
625b02c602fSJeffle Xu 
626b02c602fSJeffle Xu static inline int erofs_fscache_register_cookie(struct super_block *sb,
627b02c602fSJeffle Xu 						struct erofs_fscache **fscache,
6283c265d7dSJeffle Xu 						char *name, bool need_inode)
629b02c602fSJeffle Xu {
630b02c602fSJeffle Xu 	return -EOPNOTSUPP;
631b02c602fSJeffle Xu }
632b02c602fSJeffle Xu 
633b02c602fSJeffle Xu static inline void erofs_fscache_unregister_cookie(struct erofs_fscache **fscache)
634b02c602fSJeffle Xu {
635b02c602fSJeffle Xu }
636c6be2bd0SJeffle Xu #endif
637c6be2bd0SJeffle Xu 
63847e4937aSGao Xiang #define EFSCORRUPTED    EUCLEAN         /* Filesystem is corrupted */
63947e4937aSGao Xiang 
64047e4937aSGao Xiang #endif	/* __EROFS_INTERNAL_H */
641