xref: /openbmc/linux/fs/erofs/internal.h (revision de205114)
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;
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 
60e6242465SGao Xiang struct erofs_mount_opts {
61f57a3fe4SChao Yu #ifdef CONFIG_EROFS_FS_ZIP
62f57a3fe4SChao Yu 	/* current strategy of how to use managed cache */
63f57a3fe4SChao Yu 	unsigned char cache_strategy;
6430048cdaSHuang Jianan 	/* strategy of sync decompression (false - auto, true - force on) */
6530048cdaSHuang Jianan 	bool readahead_sync_decompress;
66f57a3fe4SChao Yu 
67f57a3fe4SChao Yu 	/* threshold for decompression synchronously */
68f57a3fe4SChao Yu 	unsigned int max_sync_decompress_pages;
69f57a3fe4SChao Yu #endif
70f57a3fe4SChao Yu 	unsigned int mount_opt;
71f57a3fe4SChao Yu };
72f57a3fe4SChao Yu 
73dfeab2e9SGao Xiang struct erofs_dev_context {
74dfeab2e9SGao Xiang 	struct idr tree;
75dfeab2e9SGao Xiang 	struct rw_semaphore rwsem;
76dfeab2e9SGao Xiang 
77dfeab2e9SGao Xiang 	unsigned int extra_devices;
78dfeab2e9SGao Xiang };
79dfeab2e9SGao Xiang 
80e6242465SGao Xiang struct erofs_fs_context {
81e6242465SGao Xiang 	struct erofs_mount_opts opt;
82dfeab2e9SGao Xiang 	struct erofs_dev_context *devs;
83e6242465SGao Xiang };
84e6242465SGao Xiang 
855d50538fSHuang Jianan /* all filesystem-wide lz4 configurations */
865d50538fSHuang Jianan struct erofs_sb_lz4_info {
875d50538fSHuang Jianan 	/* # of pages needed for EROFS lz4 rolling decompression */
885d50538fSHuang Jianan 	u16 max_distance_pages;
894fea63f7SGao Xiang 	/* maximum possible blocks for pclusters in the filesystem */
904fea63f7SGao Xiang 	u16 max_pclusterblks;
915d50538fSHuang Jianan };
925d50538fSHuang Jianan 
9347e4937aSGao Xiang struct erofs_sb_info {
94e6242465SGao Xiang 	struct erofs_mount_opts opt;	/* options */
9547e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
9647e4937aSGao Xiang 	/* list for all registered superblocks, mainly for shrinker */
9747e4937aSGao Xiang 	struct list_head list;
9847e4937aSGao Xiang 	struct mutex umount_mutex;
9947e4937aSGao Xiang 
10064094a04SGao Xiang 	/* managed XArray arranged in physical block number */
10164094a04SGao Xiang 	struct xarray managed_pslots;
10247e4937aSGao Xiang 
10347e4937aSGao Xiang 	unsigned int shrinker_run_no;
10414373711SGao Xiang 	u16 available_compr_algs;
10547e4937aSGao Xiang 
10647e4937aSGao Xiang 	/* pseudo inode to manage cached pages */
10747e4937aSGao Xiang 	struct inode *managed_cache;
1085d50538fSHuang Jianan 
1095d50538fSHuang Jianan 	struct erofs_sb_lz4_info lz4;
11047e4937aSGao Xiang #endif	/* CONFIG_EROFS_FS_ZIP */
111dfeab2e9SGao Xiang 	struct erofs_dev_context *devs;
11206252e9cSGao Xiang 	struct dax_device *dax_dev;
113cd913c76SChristoph Hellwig 	u64 dax_part_off;
114dfeab2e9SGao Xiang 	u64 total_blocks;
115dfeab2e9SGao Xiang 	u32 primarydevice_blocks;
116dfeab2e9SGao Xiang 
11747e4937aSGao Xiang 	u32 meta_blkaddr;
11847e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_XATTR
11947e4937aSGao Xiang 	u32 xattr_blkaddr;
12047e4937aSGao Xiang #endif
121dfeab2e9SGao Xiang 	u16 device_id_mask;	/* valid bits of device id to be used */
12247e4937aSGao Xiang 
12347e4937aSGao Xiang 	/* inode slot unit size in bit shift */
12447e4937aSGao Xiang 	unsigned char islotbits;
12547e4937aSGao Xiang 
12614373711SGao Xiang 	u32 sb_size;			/* total superblock size */
12747e4937aSGao Xiang 	u32 build_time_nsec;
12847e4937aSGao Xiang 	u64 build_time;
12947e4937aSGao Xiang 
13047e4937aSGao Xiang 	/* what we really care is nid, rather than ino.. */
13147e4937aSGao Xiang 	erofs_nid_t root_nid;
13247e4937aSGao Xiang 	/* used for statfs, f_files - f_favail */
13347e4937aSGao Xiang 	u64 inos;
13447e4937aSGao Xiang 
13547e4937aSGao Xiang 	u8 uuid[16];                    /* 128-bit uuid for volume */
13647e4937aSGao Xiang 	u8 volume_name[16];             /* volume name */
137b858a484SPratik Shinde 	u32 feature_compat;
138426a9308SGao Xiang 	u32 feature_incompat;
13947e4937aSGao Xiang };
14047e4937aSGao Xiang 
14147e4937aSGao Xiang #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
14247e4937aSGao Xiang #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
14347e4937aSGao Xiang 
14447e4937aSGao Xiang /* Mount flags set via mount options or defaults */
14547e4937aSGao Xiang #define EROFS_MOUNT_XATTR_USER		0x00000010
14647e4937aSGao Xiang #define EROFS_MOUNT_POSIX_ACL		0x00000020
14706252e9cSGao Xiang #define EROFS_MOUNT_DAX_ALWAYS		0x00000040
14806252e9cSGao Xiang #define EROFS_MOUNT_DAX_NEVER		0x00000080
14947e4937aSGao Xiang 
150e6242465SGao Xiang #define clear_opt(opt, option)	((opt)->mount_opt &= ~EROFS_MOUNT_##option)
151e6242465SGao Xiang #define set_opt(opt, option)	((opt)->mount_opt |= EROFS_MOUNT_##option)
152e6242465SGao Xiang #define test_opt(opt, option)	((opt)->mount_opt & EROFS_MOUNT_##option)
15347e4937aSGao Xiang 
15447e4937aSGao Xiang enum {
15547e4937aSGao Xiang 	EROFS_ZIP_CACHE_DISABLED,
15647e4937aSGao Xiang 	EROFS_ZIP_CACHE_READAHEAD,
15747e4937aSGao Xiang 	EROFS_ZIP_CACHE_READAROUND
15847e4937aSGao Xiang };
15947e4937aSGao Xiang 
160f57a3fe4SChao Yu #ifdef CONFIG_EROFS_FS_ZIP
16147e4937aSGao Xiang #define EROFS_LOCKED_MAGIC     (INT_MIN | 0xE0F510CCL)
16247e4937aSGao Xiang 
16347e4937aSGao Xiang /* basic unit of the workstation of a super_block */
16447e4937aSGao Xiang struct erofs_workgroup {
16547e4937aSGao Xiang 	/* the workgroup index in the workstation */
16647e4937aSGao Xiang 	pgoff_t index;
16747e4937aSGao Xiang 
16847e4937aSGao Xiang 	/* overall workgroup reference count */
16947e4937aSGao Xiang 	atomic_t refcount;
17047e4937aSGao Xiang };
17147e4937aSGao Xiang 
17247e4937aSGao Xiang #if defined(CONFIG_SMP)
17347e4937aSGao Xiang static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
17447e4937aSGao Xiang 						 int val)
17547e4937aSGao Xiang {
17647e4937aSGao Xiang 	preempt_disable();
17747e4937aSGao Xiang 	if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
17847e4937aSGao Xiang 		preempt_enable();
17947e4937aSGao Xiang 		return false;
18047e4937aSGao Xiang 	}
18147e4937aSGao Xiang 	return true;
18247e4937aSGao Xiang }
18347e4937aSGao Xiang 
18447e4937aSGao Xiang static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
18547e4937aSGao Xiang 					    int orig_val)
18647e4937aSGao Xiang {
18747e4937aSGao Xiang 	/*
18847e4937aSGao Xiang 	 * other observers should notice all modifications
18947e4937aSGao Xiang 	 * in the freezing period.
19047e4937aSGao Xiang 	 */
19147e4937aSGao Xiang 	smp_mb();
19247e4937aSGao Xiang 	atomic_set(&grp->refcount, orig_val);
19347e4937aSGao Xiang 	preempt_enable();
19447e4937aSGao Xiang }
19547e4937aSGao Xiang 
19647e4937aSGao Xiang static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
19747e4937aSGao Xiang {
19847e4937aSGao Xiang 	return atomic_cond_read_relaxed(&grp->refcount,
19947e4937aSGao Xiang 					VAL != EROFS_LOCKED_MAGIC);
20047e4937aSGao Xiang }
20147e4937aSGao Xiang #else
20247e4937aSGao Xiang static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
20347e4937aSGao Xiang 						 int val)
20447e4937aSGao Xiang {
20547e4937aSGao Xiang 	preempt_disable();
20647e4937aSGao Xiang 	/* no need to spin on UP platforms, let's just disable preemption. */
20747e4937aSGao Xiang 	if (val != atomic_read(&grp->refcount)) {
20847e4937aSGao Xiang 		preempt_enable();
20947e4937aSGao Xiang 		return false;
21047e4937aSGao Xiang 	}
21147e4937aSGao Xiang 	return true;
21247e4937aSGao Xiang }
21347e4937aSGao Xiang 
21447e4937aSGao Xiang static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
21547e4937aSGao Xiang 					    int orig_val)
21647e4937aSGao Xiang {
21747e4937aSGao Xiang 	preempt_enable();
21847e4937aSGao Xiang }
21947e4937aSGao Xiang 
22047e4937aSGao Xiang static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
22147e4937aSGao Xiang {
22247e4937aSGao Xiang 	int v = atomic_read(&grp->refcount);
22347e4937aSGao Xiang 
22447e4937aSGao Xiang 	/* workgroup is never freezed on uniprocessor systems */
22547e4937aSGao Xiang 	DBG_BUGON(v == EROFS_LOCKED_MAGIC);
22647e4937aSGao Xiang 	return v;
22747e4937aSGao Xiang }
22847e4937aSGao Xiang #endif	/* !CONFIG_SMP */
22947e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_ZIP */
23047e4937aSGao Xiang 
23147e4937aSGao Xiang /* we strictly follow PAGE_SIZE and no buffer head yet */
23247e4937aSGao Xiang #define LOG_BLOCK_SIZE		PAGE_SHIFT
23347e4937aSGao Xiang 
23447e4937aSGao Xiang #undef LOG_SECTORS_PER_BLOCK
23547e4937aSGao Xiang #define LOG_SECTORS_PER_BLOCK	(PAGE_SHIFT - 9)
23647e4937aSGao Xiang 
23747e4937aSGao Xiang #undef SECTORS_PER_BLOCK
23847e4937aSGao Xiang #define SECTORS_PER_BLOCK	(1 << SECTORS_PER_BLOCK)
23947e4937aSGao Xiang 
24047e4937aSGao Xiang #define EROFS_BLKSIZ		(1 << LOG_BLOCK_SIZE)
24147e4937aSGao Xiang 
24247e4937aSGao Xiang #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
24347e4937aSGao Xiang #error erofs cannot be used in this platform
24447e4937aSGao Xiang #endif
24547e4937aSGao Xiang 
24647e4937aSGao Xiang #define ROOT_NID(sb)		((sb)->root_nid)
24747e4937aSGao Xiang 
24847e4937aSGao Xiang #define erofs_blknr(addr)       ((addr) / EROFS_BLKSIZ)
24947e4937aSGao Xiang #define erofs_blkoff(addr)      ((addr) % EROFS_BLKSIZ)
25047e4937aSGao Xiang #define blknr_to_addr(nr)       ((erofs_off_t)(nr) * EROFS_BLKSIZ)
25147e4937aSGao Xiang 
25247e4937aSGao Xiang static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
25347e4937aSGao Xiang {
25447e4937aSGao Xiang 	return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
25547e4937aSGao Xiang }
25647e4937aSGao Xiang 
257de06a6a3SGao Xiang #define EROFS_FEATURE_FUNCS(name, compat, feature) \
258de06a6a3SGao Xiang static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
259de06a6a3SGao Xiang { \
260de06a6a3SGao Xiang 	return sbi->feature_##compat & EROFS_FEATURE_##feature; \
261de06a6a3SGao Xiang }
262de06a6a3SGao Xiang 
263de06a6a3SGao Xiang EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING)
26414373711SGao Xiang EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
2655404c330SGao Xiang EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
266dfeab2e9SGao Xiang EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
267de06a6a3SGao Xiang EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
268de06a6a3SGao Xiang 
26947e4937aSGao Xiang /* atomic flag definitions */
270a5876e24SGao Xiang #define EROFS_I_EA_INITED_BIT	0
271a5876e24SGao Xiang #define EROFS_I_Z_INITED_BIT	1
27247e4937aSGao Xiang 
27347e4937aSGao Xiang /* bitlock definitions (arranged in reverse order) */
274a5876e24SGao Xiang #define EROFS_I_BL_XATTR_BIT	(BITS_PER_LONG - 1)
275a5876e24SGao Xiang #define EROFS_I_BL_Z_BIT	(BITS_PER_LONG - 2)
27647e4937aSGao Xiang 
277a5876e24SGao Xiang struct erofs_inode {
27847e4937aSGao Xiang 	erofs_nid_t nid;
27947e4937aSGao Xiang 
28047e4937aSGao Xiang 	/* atomic flags (including bitlocks) */
28147e4937aSGao Xiang 	unsigned long flags;
28247e4937aSGao Xiang 
2838a765682SGao Xiang 	unsigned char datalayout;
28447e4937aSGao Xiang 	unsigned char inode_isize;
28547e4937aSGao Xiang 	unsigned short xattr_isize;
28647e4937aSGao Xiang 
28747e4937aSGao Xiang 	unsigned int xattr_shared_count;
28847e4937aSGao Xiang 	unsigned int *xattr_shared_xattrs;
28947e4937aSGao Xiang 
29047e4937aSGao Xiang 	union {
29147e4937aSGao Xiang 		erofs_blk_t raw_blkaddr;
292c5aa903aSGao Xiang 		struct {
293c5aa903aSGao Xiang 			unsigned short	chunkformat;
294c5aa903aSGao Xiang 			unsigned char	chunkbits;
295c5aa903aSGao Xiang 		};
29647e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
29747e4937aSGao Xiang 		struct {
29847e4937aSGao Xiang 			unsigned short z_advise;
29947e4937aSGao Xiang 			unsigned char  z_algorithmtype[2];
30047e4937aSGao Xiang 			unsigned char  z_logical_clusterbits;
30147e4937aSGao Xiang 		};
30247e4937aSGao Xiang #endif	/* CONFIG_EROFS_FS_ZIP */
30347e4937aSGao Xiang 	};
30447e4937aSGao Xiang 	/* the corresponding vfs inode */
30547e4937aSGao Xiang 	struct inode vfs_inode;
30647e4937aSGao Xiang };
30747e4937aSGao Xiang 
308a5876e24SGao Xiang #define EROFS_I(ptr)	\
309a5876e24SGao Xiang 	container_of(ptr, struct erofs_inode, vfs_inode)
31047e4937aSGao Xiang 
31199634bf3SGao Xiang static inline unsigned long erofs_inode_datablocks(struct inode *inode)
31247e4937aSGao Xiang {
31347e4937aSGao Xiang 	/* since i_size cannot be changed */
31447e4937aSGao Xiang 	return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
31547e4937aSGao Xiang }
31647e4937aSGao Xiang 
3178a765682SGao Xiang static inline unsigned int erofs_bitrange(unsigned int value, unsigned int bit,
3188a765682SGao Xiang 					  unsigned int bits)
31947e4937aSGao Xiang {
3208a765682SGao Xiang 
3218a765682SGao Xiang 	return (value >> bit) & ((1 << bits) - 1);
32247e4937aSGao Xiang }
32347e4937aSGao Xiang 
3248a765682SGao Xiang 
3258a765682SGao Xiang static inline unsigned int erofs_inode_version(unsigned int value)
32647e4937aSGao Xiang {
3278a765682SGao Xiang 	return erofs_bitrange(value, EROFS_I_VERSION_BIT,
3288a765682SGao Xiang 			      EROFS_I_VERSION_BITS);
3298a765682SGao Xiang }
3308a765682SGao Xiang 
3318a765682SGao Xiang static inline unsigned int erofs_inode_datalayout(unsigned int value)
3328a765682SGao Xiang {
3338a765682SGao Xiang 	return erofs_bitrange(value, EROFS_I_DATALAYOUT_BIT,
3348a765682SGao Xiang 			      EROFS_I_DATALAYOUT_BITS);
33547e4937aSGao Xiang }
33647e4937aSGao Xiang 
33738629291SGao Xiang /*
33838629291SGao Xiang  * Different from grab_cache_page_nowait(), reclaiming is never triggered
33938629291SGao Xiang  * when allocating new pages.
34038629291SGao Xiang  */
34138629291SGao Xiang static inline
34238629291SGao Xiang struct page *erofs_grab_cache_page_nowait(struct address_space *mapping,
34338629291SGao Xiang 					  pgoff_t index)
34438629291SGao Xiang {
34538629291SGao Xiang 	return pagecache_get_page(mapping, index,
34638629291SGao Xiang 			FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
34738629291SGao Xiang 			readahead_gfp_mask(mapping) & ~__GFP_RECLAIM);
34838629291SGao Xiang }
34938629291SGao Xiang 
35047e4937aSGao Xiang extern const struct super_operations erofs_sops;
35147e4937aSGao Xiang 
35247e4937aSGao Xiang extern const struct address_space_operations erofs_raw_access_aops;
3530c638f70SGao Xiang extern const struct address_space_operations z_erofs_aops;
35447e4937aSGao Xiang 
35547e4937aSGao Xiang /*
3568137824eSYue Hu  * Logical to physical block mapping
35747e4937aSGao Xiang  *
35847e4937aSGao Xiang  * Different with other file systems, it is used for 2 access modes:
35947e4937aSGao Xiang  *
36047e4937aSGao Xiang  * 1) RAW access mode:
36147e4937aSGao Xiang  *
36247e4937aSGao Xiang  * Users pass a valid (m_lblk, m_lofs -- usually 0) pair,
36347e4937aSGao Xiang  * and get the valid m_pblk, m_pofs and the longest m_len(in bytes).
36447e4937aSGao Xiang  *
36547e4937aSGao Xiang  * Note that m_lblk in the RAW access mode refers to the number of
36647e4937aSGao Xiang  * the compressed ondisk block rather than the uncompressed
36747e4937aSGao Xiang  * in-memory block for the compressed file.
36847e4937aSGao Xiang  *
36947e4937aSGao Xiang  * m_pofs equals to m_lofs except for the inline data page.
37047e4937aSGao Xiang  *
37147e4937aSGao Xiang  * 2) Normal access mode:
37247e4937aSGao Xiang  *
37347e4937aSGao Xiang  * If the inode is not compressed, it has no difference with
37447e4937aSGao Xiang  * the RAW access mode. However, if the inode is compressed,
37547e4937aSGao Xiang  * users should pass a valid (m_lblk, m_lofs) pair, and get
37647e4937aSGao Xiang  * the needed m_pblk, m_pofs, m_len to get the compressed data
37747e4937aSGao Xiang  * and the updated m_lblk, m_lofs which indicates the start
37847e4937aSGao Xiang  * of the corresponding uncompressed data in the file.
37947e4937aSGao Xiang  */
38047e4937aSGao Xiang enum {
3818f899262SGao Xiang 	BH_Encoded = BH_PrivateStart,
38247e4937aSGao Xiang 	BH_FullMapped,
38347e4937aSGao Xiang };
38447e4937aSGao Xiang 
38547e4937aSGao Xiang /* Has a disk mapping */
38647e4937aSGao Xiang #define EROFS_MAP_MAPPED	(1 << BH_Mapped)
38747e4937aSGao Xiang /* Located in metadata (could be copied from bd_inode) */
38847e4937aSGao Xiang #define EROFS_MAP_META		(1 << BH_Meta)
3898f899262SGao Xiang /* The extent is encoded */
3908f899262SGao Xiang #define EROFS_MAP_ENCODED	(1 << BH_Encoded)
39147e4937aSGao Xiang /* The length of extent is full */
39247e4937aSGao Xiang #define EROFS_MAP_FULL_MAPPED	(1 << BH_FullMapped)
39347e4937aSGao Xiang 
39447e4937aSGao Xiang struct erofs_map_blocks {
39547e4937aSGao Xiang 	erofs_off_t m_pa, m_la;
39647e4937aSGao Xiang 	u64 m_plen, m_llen;
39747e4937aSGao Xiang 
398dfeab2e9SGao Xiang 	unsigned short m_deviceid;
3998f899262SGao Xiang 	char m_algorithmformat;
40047e4937aSGao Xiang 	unsigned int m_flags;
40147e4937aSGao Xiang 
40247e4937aSGao Xiang 	struct page *mpage;
40347e4937aSGao Xiang };
40447e4937aSGao Xiang 
4058137824eSYue Hu /* Flags used by erofs_map_blocks_flatmode() */
40647e4937aSGao Xiang #define EROFS_GET_BLOCKS_RAW    0x0001
407d95ae5e2SGao Xiang /*
408d95ae5e2SGao Xiang  * Used to get the exact decompressed length, e.g. fiemap (consider lookback
409d95ae5e2SGao Xiang  * approach instead if possible since it's more metadata lightweight.)
410d95ae5e2SGao Xiang  */
411d95ae5e2SGao Xiang #define EROFS_GET_BLOCKS_FIEMAP	0x0002
412622ceaddSGao Xiang /* Used to map the whole extent if non-negligible data is requested for LZMA */
413622ceaddSGao Xiang #define EROFS_GET_BLOCKS_READMORE	0x0004
41447e4937aSGao Xiang 
4158f899262SGao Xiang enum {
4168f899262SGao Xiang 	Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
4178f899262SGao Xiang 	Z_EROFS_COMPRESSION_RUNTIME_MAX
4188f899262SGao Xiang };
4198f899262SGao Xiang 
42047e4937aSGao Xiang /* zmap.c */
421eadcd6b5SGao Xiang extern const struct iomap_ops z_erofs_iomap_report_ops;
422eadcd6b5SGao Xiang 
42347e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
42447e4937aSGao Xiang int z_erofs_fill_inode(struct inode *inode);
42547e4937aSGao Xiang int z_erofs_map_blocks_iter(struct inode *inode,
42647e4937aSGao Xiang 			    struct erofs_map_blocks *map,
42747e4937aSGao Xiang 			    int flags);
42847e4937aSGao Xiang #else
42947e4937aSGao Xiang static inline int z_erofs_fill_inode(struct inode *inode) { return -EOPNOTSUPP; }
43047e4937aSGao Xiang static inline int z_erofs_map_blocks_iter(struct inode *inode,
43147e4937aSGao Xiang 					  struct erofs_map_blocks *map,
43247e4937aSGao Xiang 					  int flags)
43347e4937aSGao Xiang {
43447e4937aSGao Xiang 	return -EOPNOTSUPP;
43547e4937aSGao Xiang }
43647e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_ZIP */
43747e4937aSGao Xiang 
438dfeab2e9SGao Xiang struct erofs_map_dev {
439dfeab2e9SGao Xiang 	struct block_device *m_bdev;
440dfeab2e9SGao Xiang 	struct dax_device *m_daxdev;
441*de205114SChristoph Hellwig 	u64 m_dax_part_off;
442dfeab2e9SGao Xiang 
443dfeab2e9SGao Xiang 	erofs_off_t m_pa;
444dfeab2e9SGao Xiang 	unsigned int m_deviceid;
445dfeab2e9SGao Xiang };
446dfeab2e9SGao Xiang 
44747e4937aSGao Xiang /* data.c */
448a08e67a0SHuang Jianan extern const struct file_operations erofs_file_fops;
449e655b5b3SGao Xiang struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr);
450dfeab2e9SGao Xiang int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
451eadcd6b5SGao Xiang int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
452eadcd6b5SGao Xiang 		 u64 start, u64 len);
45347e4937aSGao Xiang 
45447e4937aSGao Xiang /* inode.c */
45547e4937aSGao Xiang static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
45647e4937aSGao Xiang {
45747e4937aSGao Xiang #if BITS_PER_LONG == 32
45847e4937aSGao Xiang 	return (nid >> 32) ^ (nid & 0xffffffff);
45947e4937aSGao Xiang #else
46047e4937aSGao Xiang 	return nid;
46147e4937aSGao Xiang #endif
46247e4937aSGao Xiang }
46347e4937aSGao Xiang 
46447e4937aSGao Xiang extern const struct inode_operations erofs_generic_iops;
46547e4937aSGao Xiang extern const struct inode_operations erofs_symlink_iops;
46647e4937aSGao Xiang extern const struct inode_operations erofs_fast_symlink_iops;
46747e4937aSGao Xiang 
46847e4937aSGao Xiang struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir);
469549c7297SChristian Brauner int erofs_getattr(struct user_namespace *mnt_userns, const struct path *path,
470549c7297SChristian Brauner 		  struct kstat *stat, u32 request_mask,
471549c7297SChristian Brauner 		  unsigned int query_flags);
47247e4937aSGao Xiang 
47347e4937aSGao Xiang /* namei.c */
47447e4937aSGao Xiang extern const struct inode_operations erofs_dir_iops;
47547e4937aSGao Xiang 
47647e4937aSGao Xiang int erofs_namei(struct inode *dir, struct qstr *name,
47747e4937aSGao Xiang 		erofs_nid_t *nid, unsigned int *d_type);
47847e4937aSGao Xiang 
47947e4937aSGao Xiang /* dir.c */
48047e4937aSGao Xiang extern const struct file_operations erofs_dir_fops;
48147e4937aSGao Xiang 
482598162d0SGao Xiang static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
483598162d0SGao Xiang {
484598162d0SGao Xiang 	int retried = 0;
485598162d0SGao Xiang 
486598162d0SGao Xiang 	while (1) {
487598162d0SGao Xiang 		void *p = vm_map_ram(pages, count, -1);
488598162d0SGao Xiang 
489598162d0SGao Xiang 		/* retry two more times (totally 3 times) */
490598162d0SGao Xiang 		if (p || ++retried >= 3)
491598162d0SGao Xiang 			return p;
492598162d0SGao Xiang 		vm_unmap_aliases();
493598162d0SGao Xiang 	}
494598162d0SGao Xiang 	return NULL;
495598162d0SGao Xiang }
496598162d0SGao Xiang 
49752488734SGao Xiang /* pcpubuf.c */
49852488734SGao Xiang void *erofs_get_pcpubuf(unsigned int requiredpages);
49952488734SGao Xiang void erofs_put_pcpubuf(void *ptr);
50052488734SGao Xiang int erofs_pcpubuf_growsize(unsigned int nrpages);
50152488734SGao Xiang void erofs_pcpubuf_init(void);
50252488734SGao Xiang void erofs_pcpubuf_exit(void);
50352488734SGao Xiang 
50447e4937aSGao Xiang /* utils.c / zdata.c */
505eaa9172aSGao Xiang struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
506eaa9172aSGao Xiang static inline void erofs_pagepool_add(struct page **pagepool,
507eaa9172aSGao Xiang 		struct page *page)
508eaa9172aSGao Xiang {
509eaa9172aSGao Xiang 	set_page_private(page, (unsigned long)*pagepool);
510eaa9172aSGao Xiang 	*pagepool = page;
511eaa9172aSGao Xiang }
512eaa9172aSGao Xiang void erofs_release_pages(struct page **pagepool);
51347e4937aSGao Xiang 
51447e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP
51547e4937aSGao Xiang int erofs_workgroup_put(struct erofs_workgroup *grp);
51647e4937aSGao Xiang struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
517997626d8SVladimir Zapolskiy 					     pgoff_t index);
51864094a04SGao Xiang struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
519e5e9a432SVladimir Zapolskiy 					       struct erofs_workgroup *grp);
52047e4937aSGao Xiang void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
52147e4937aSGao Xiang void erofs_shrinker_register(struct super_block *sb);
52247e4937aSGao Xiang void erofs_shrinker_unregister(struct super_block *sb);
52347e4937aSGao Xiang int __init erofs_init_shrinker(void);
52447e4937aSGao Xiang void erofs_exit_shrinker(void);
52547e4937aSGao Xiang int __init z_erofs_init_zip_subsystem(void);
52647e4937aSGao Xiang void z_erofs_exit_zip_subsystem(void);
52747e4937aSGao Xiang int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
52847e4937aSGao Xiang 				       struct erofs_workgroup *egrp);
529d252ff3dSYue Hu int erofs_try_to_free_cached_page(struct page *page);
5305d50538fSHuang Jianan int z_erofs_load_lz4_config(struct super_block *sb,
53146249cdeSGao Xiang 			    struct erofs_super_block *dsb,
53246249cdeSGao Xiang 			    struct z_erofs_lz4_cfgs *lz4, int len);
53347e4937aSGao Xiang #else
53447e4937aSGao Xiang static inline void erofs_shrinker_register(struct super_block *sb) {}
53547e4937aSGao Xiang static inline void erofs_shrinker_unregister(struct super_block *sb) {}
53647e4937aSGao Xiang static inline int erofs_init_shrinker(void) { return 0; }
53747e4937aSGao Xiang static inline void erofs_exit_shrinker(void) {}
53847e4937aSGao Xiang static inline int z_erofs_init_zip_subsystem(void) { return 0; }
53947e4937aSGao Xiang static inline void z_erofs_exit_zip_subsystem(void) {}
5405d50538fSHuang Jianan static inline int z_erofs_load_lz4_config(struct super_block *sb,
54146249cdeSGao Xiang 				  struct erofs_super_block *dsb,
54246249cdeSGao Xiang 				  struct z_erofs_lz4_cfgs *lz4, int len)
5435d50538fSHuang Jianan {
54414373711SGao Xiang 	if (lz4 || dsb->u1.lz4_max_distance) {
5455d50538fSHuang Jianan 		erofs_err(sb, "lz4 algorithm isn't enabled");
5465d50538fSHuang Jianan 		return -EINVAL;
5475d50538fSHuang Jianan 	}
5485d50538fSHuang Jianan 	return 0;
5495d50538fSHuang Jianan }
55047e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_ZIP */
55147e4937aSGao Xiang 
552622ceaddSGao Xiang #ifdef CONFIG_EROFS_FS_ZIP_LZMA
553622ceaddSGao Xiang int z_erofs_lzma_init(void);
554622ceaddSGao Xiang void z_erofs_lzma_exit(void);
555622ceaddSGao Xiang int z_erofs_load_lzma_config(struct super_block *sb,
556622ceaddSGao Xiang 			     struct erofs_super_block *dsb,
557622ceaddSGao Xiang 			     struct z_erofs_lzma_cfgs *lzma, int size);
558622ceaddSGao Xiang #else
559622ceaddSGao Xiang static inline int z_erofs_lzma_init(void) { return 0; }
560622ceaddSGao Xiang static inline int z_erofs_lzma_exit(void) { return 0; }
561622ceaddSGao Xiang static inline int z_erofs_load_lzma_config(struct super_block *sb,
562622ceaddSGao Xiang 			     struct erofs_super_block *dsb,
563622ceaddSGao Xiang 			     struct z_erofs_lzma_cfgs *lzma, int size) {
564622ceaddSGao Xiang 	if (lzma) {
565622ceaddSGao Xiang 		erofs_err(sb, "lzma algorithm isn't enabled");
566622ceaddSGao Xiang 		return -EINVAL;
567622ceaddSGao Xiang 	}
568622ceaddSGao Xiang 	return 0;
569622ceaddSGao Xiang }
570622ceaddSGao Xiang #endif	/* !CONFIG_EROFS_FS_ZIP */
571622ceaddSGao Xiang 
57247e4937aSGao Xiang #define EFSCORRUPTED    EUCLEAN         /* Filesystem is corrupted */
57347e4937aSGao Xiang 
57447e4937aSGao Xiang #endif	/* __EROFS_INTERNAL_H */
575