1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2021, Alibaba Cloud
7 #ifndef __EROFS_INTERNAL_H
8 #define __EROFS_INTERNAL_H
11 #include <linux/dcache.h>
13 #include <linux/pagemap.h>
14 #include <linux/bio.h>
15 #include <linux/buffer_head.h>
16 #include <linux/magic.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/iomap.h>
22 /* redefine pr_fmt "erofs: " */
24 #define pr_fmt(fmt) "erofs: " fmt
26 __printf(3, 4) void _erofs_err(struct super_block *sb,
27 const char *function, const char *fmt, ...);
28 #define erofs_err(sb, fmt, ...) \
29 _erofs_err(sb, __func__, fmt "\n", ##__VA_ARGS__)
30 __printf(3, 4) void _erofs_info(struct super_block *sb,
31 const char *function, const char *fmt, ...);
32 #define erofs_info(sb, fmt, ...) \
33 _erofs_info(sb, __func__, fmt "\n", ##__VA_ARGS__)
34 #ifdef CONFIG_EROFS_FS_DEBUG
35 #define erofs_dbg(x, ...) pr_debug(x "\n", ##__VA_ARGS__)
36 #define DBG_BUGON BUG_ON
38 #define erofs_dbg(x, ...) ((void)0)
39 #define DBG_BUGON(x) ((void)(x))
40 #endif /* !CONFIG_EROFS_FS_DEBUG */
42 /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
43 #define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
45 typedef u64 erofs_nid_t;
46 typedef u64 erofs_off_t;
47 /* data type for filesystem-wide blocks number */
48 typedef u32 erofs_blk_t;
50 struct erofs_mount_opts {
51 #ifdef CONFIG_EROFS_FS_ZIP
52 /* current strategy of how to use managed cache */
53 unsigned char cache_strategy;
54 /* strategy of sync decompression (false - auto, true - force on) */
55 bool readahead_sync_decompress;
57 /* threshold for decompression synchronously */
58 unsigned int max_sync_decompress_pages;
60 unsigned int mount_opt;
63 struct erofs_fs_context {
64 struct erofs_mount_opts opt;
67 /* all filesystem-wide lz4 configurations */
68 struct erofs_sb_lz4_info {
69 /* # of pages needed for EROFS lz4 rolling decompression */
70 u16 max_distance_pages;
71 /* maximum possible blocks for pclusters in the filesystem */
75 struct erofs_sb_info {
76 struct erofs_mount_opts opt; /* options */
78 #ifdef CONFIG_EROFS_FS_ZIP
79 /* list for all registered superblocks, mainly for shrinker */
80 struct list_head list;
81 struct mutex umount_mutex;
83 /* managed XArray arranged in physical block number */
84 struct xarray managed_pslots;
86 unsigned int shrinker_run_no;
87 u16 available_compr_algs;
89 /* pseudo inode to manage cached pages */
90 struct inode *managed_cache;
92 struct erofs_sb_lz4_info lz4;
93 #endif /* CONFIG_EROFS_FS_ZIP */
94 struct dax_device *dax_dev;
97 #ifdef CONFIG_EROFS_FS_XATTR
101 /* inode slot unit size in bit shift */
102 unsigned char islotbits;
104 u32 sb_size; /* total superblock size */
108 /* what we really care is nid, rather than ino.. */
109 erofs_nid_t root_nid;
110 /* used for statfs, f_files - f_favail */
113 u8 uuid[16]; /* 128-bit uuid for volume */
114 u8 volume_name[16]; /* volume name */
116 u32 feature_incompat;
119 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
120 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
122 /* Mount flags set via mount options or defaults */
123 #define EROFS_MOUNT_XATTR_USER 0x00000010
124 #define EROFS_MOUNT_POSIX_ACL 0x00000020
125 #define EROFS_MOUNT_DAX_ALWAYS 0x00000040
126 #define EROFS_MOUNT_DAX_NEVER 0x00000080
128 #define clear_opt(opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)
129 #define set_opt(opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)
130 #define test_opt(opt, option) ((opt)->mount_opt & EROFS_MOUNT_##option)
133 EROFS_ZIP_CACHE_DISABLED,
134 EROFS_ZIP_CACHE_READAHEAD,
135 EROFS_ZIP_CACHE_READAROUND
138 #ifdef CONFIG_EROFS_FS_ZIP
139 #define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL)
141 /* basic unit of the workstation of a super_block */
142 struct erofs_workgroup {
143 /* the workgroup index in the workstation */
146 /* overall workgroup reference count */
150 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
154 if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
161 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
165 * other observers should notice all modifications
166 * in the freezing period.
169 atomic_set(&grp->refcount, orig_val);
173 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
175 return atomic_cond_read_relaxed(&grp->refcount,
176 VAL != EROFS_LOCKED_MAGIC);
178 #endif /* !CONFIG_EROFS_FS_ZIP */
180 /* we strictly follow PAGE_SIZE and no buffer head yet */
181 #define LOG_BLOCK_SIZE PAGE_SHIFT
183 #undef LOG_SECTORS_PER_BLOCK
184 #define LOG_SECTORS_PER_BLOCK (PAGE_SHIFT - 9)
186 #undef SECTORS_PER_BLOCK
187 #define SECTORS_PER_BLOCK (1 << SECTORS_PER_BLOCK)
189 #define EROFS_BLKSIZ (1 << LOG_BLOCK_SIZE)
191 #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
192 #error erofs cannot be used in this platform
195 #define ROOT_NID(sb) ((sb)->root_nid)
197 #define erofs_blknr(addr) ((addr) / EROFS_BLKSIZ)
198 #define erofs_blkoff(addr) ((addr) % EROFS_BLKSIZ)
199 #define blknr_to_addr(nr) ((erofs_off_t)(nr) * EROFS_BLKSIZ)
201 static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
203 return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
206 #define EROFS_FEATURE_FUNCS(name, compat, feature) \
207 static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
209 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
212 EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING)
213 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
214 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
215 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
217 /* atomic flag definitions */
218 #define EROFS_I_EA_INITED_BIT 0
219 #define EROFS_I_Z_INITED_BIT 1
221 /* bitlock definitions (arranged in reverse order) */
222 #define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
223 #define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
228 /* atomic flags (including bitlocks) */
231 unsigned char datalayout;
232 unsigned char inode_isize;
233 unsigned int xattr_isize;
235 unsigned int xattr_shared_count;
236 unsigned int *xattr_shared_xattrs;
239 erofs_blk_t raw_blkaddr;
241 unsigned short chunkformat;
242 unsigned char chunkbits;
244 #ifdef CONFIG_EROFS_FS_ZIP
246 unsigned short z_advise;
247 unsigned char z_algorithmtype[2];
248 unsigned char z_logical_clusterbits;
250 #endif /* CONFIG_EROFS_FS_ZIP */
252 /* the corresponding vfs inode */
253 struct inode vfs_inode;
256 #define EROFS_I(ptr) \
257 container_of(ptr, struct erofs_inode, vfs_inode)
259 static inline unsigned long erofs_inode_datablocks(struct inode *inode)
261 /* since i_size cannot be changed */
262 return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
265 static inline unsigned int erofs_bitrange(unsigned int value, unsigned int bit,
269 return (value >> bit) & ((1 << bits) - 1);
273 static inline unsigned int erofs_inode_version(unsigned int value)
275 return erofs_bitrange(value, EROFS_I_VERSION_BIT,
276 EROFS_I_VERSION_BITS);
279 static inline unsigned int erofs_inode_datalayout(unsigned int value)
281 return erofs_bitrange(value, EROFS_I_DATALAYOUT_BIT,
282 EROFS_I_DATALAYOUT_BITS);
285 extern const struct super_operations erofs_sops;
287 extern const struct address_space_operations erofs_raw_access_aops;
288 extern const struct address_space_operations z_erofs_aops;
291 * Logical to physical block mapping
293 * Different with other file systems, it is used for 2 access modes:
295 * 1) RAW access mode:
297 * Users pass a valid (m_lblk, m_lofs -- usually 0) pair,
298 * and get the valid m_pblk, m_pofs and the longest m_len(in bytes).
300 * Note that m_lblk in the RAW access mode refers to the number of
301 * the compressed ondisk block rather than the uncompressed
302 * in-memory block for the compressed file.
304 * m_pofs equals to m_lofs except for the inline data page.
306 * 2) Normal access mode:
308 * If the inode is not compressed, it has no difference with
309 * the RAW access mode. However, if the inode is compressed,
310 * users should pass a valid (m_lblk, m_lofs) pair, and get
311 * the needed m_pblk, m_pofs, m_len to get the compressed data
312 * and the updated m_lblk, m_lofs which indicates the start
313 * of the corresponding uncompressed data in the file.
316 BH_Zipped = BH_PrivateStart,
320 /* Has a disk mapping */
321 #define EROFS_MAP_MAPPED (1 << BH_Mapped)
322 /* Located in metadata (could be copied from bd_inode) */
323 #define EROFS_MAP_META (1 << BH_Meta)
324 /* The extent has been compressed */
325 #define EROFS_MAP_ZIPPED (1 << BH_Zipped)
326 /* The length of extent is full */
327 #define EROFS_MAP_FULL_MAPPED (1 << BH_FullMapped)
329 struct erofs_map_blocks {
330 erofs_off_t m_pa, m_la;
333 unsigned int m_flags;
338 /* Flags used by erofs_map_blocks_flatmode() */
339 #define EROFS_GET_BLOCKS_RAW 0x0001
341 * Used to get the exact decompressed length, e.g. fiemap (consider lookback
342 * approach instead if possible since it's more metadata lightweight.)
344 #define EROFS_GET_BLOCKS_FIEMAP 0x0002
347 extern const struct iomap_ops z_erofs_iomap_report_ops;
349 #ifdef CONFIG_EROFS_FS_ZIP
350 int z_erofs_fill_inode(struct inode *inode);
351 int z_erofs_map_blocks_iter(struct inode *inode,
352 struct erofs_map_blocks *map,
355 static inline int z_erofs_fill_inode(struct inode *inode) { return -EOPNOTSUPP; }
356 static inline int z_erofs_map_blocks_iter(struct inode *inode,
357 struct erofs_map_blocks *map,
362 #endif /* !CONFIG_EROFS_FS_ZIP */
365 extern const struct file_operations erofs_file_fops;
366 struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr);
367 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
371 static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
373 #if BITS_PER_LONG == 32
374 return (nid >> 32) ^ (nid & 0xffffffff);
380 extern const struct inode_operations erofs_generic_iops;
381 extern const struct inode_operations erofs_symlink_iops;
382 extern const struct inode_operations erofs_fast_symlink_iops;
384 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir);
385 int erofs_getattr(struct user_namespace *mnt_userns, const struct path *path,
386 struct kstat *stat, u32 request_mask,
387 unsigned int query_flags);
390 extern const struct inode_operations erofs_dir_iops;
392 int erofs_namei(struct inode *dir, struct qstr *name,
393 erofs_nid_t *nid, unsigned int *d_type);
396 extern const struct file_operations erofs_dir_fops;
398 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
403 void *p = vm_map_ram(pages, count, -1);
405 /* retry two more times (totally 3 times) */
406 if (p || ++retried >= 3)
414 void *erofs_get_pcpubuf(unsigned int requiredpages);
415 void erofs_put_pcpubuf(void *ptr);
416 int erofs_pcpubuf_growsize(unsigned int nrpages);
417 void erofs_pcpubuf_init(void);
418 void erofs_pcpubuf_exit(void);
420 /* utils.c / zdata.c */
421 struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp);
423 #ifdef CONFIG_EROFS_FS_ZIP
424 int erofs_workgroup_put(struct erofs_workgroup *grp);
425 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
427 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
428 struct erofs_workgroup *grp);
429 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
430 void erofs_shrinker_register(struct super_block *sb);
431 void erofs_shrinker_unregister(struct super_block *sb);
432 int __init erofs_init_shrinker(void);
433 void erofs_exit_shrinker(void);
434 int __init z_erofs_init_zip_subsystem(void);
435 void z_erofs_exit_zip_subsystem(void);
436 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
437 struct erofs_workgroup *egrp);
438 int erofs_try_to_free_cached_page(struct page *page);
439 int z_erofs_load_lz4_config(struct super_block *sb,
440 struct erofs_super_block *dsb,
441 struct z_erofs_lz4_cfgs *lz4, int len);
443 static inline void erofs_shrinker_register(struct super_block *sb) {}
444 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
445 static inline int erofs_init_shrinker(void) { return 0; }
446 static inline void erofs_exit_shrinker(void) {}
447 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
448 static inline void z_erofs_exit_zip_subsystem(void) {}
449 static inline int z_erofs_load_lz4_config(struct super_block *sb,
450 struct erofs_super_block *dsb,
451 struct z_erofs_lz4_cfgs *lz4, int len)
453 if (lz4 || dsb->u1.lz4_max_distance) {
454 erofs_err(sb, "lz4 algorithm isn't enabled");
459 #endif /* !CONFIG_EROFS_FS_ZIP */
461 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
463 #endif /* __EROFS_INTERNAL_H */