1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
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>
21 /* redefine pr_fmt "erofs: " */
23 #define pr_fmt(fmt) "erofs: " fmt
25 __printf(3, 4) void _erofs_err(struct super_block *sb,
26 const char *function, const char *fmt, ...);
27 #define erofs_err(sb, fmt, ...) \
28 _erofs_err(sb, __func__, fmt "\n", ##__VA_ARGS__)
29 __printf(3, 4) void _erofs_info(struct super_block *sb,
30 const char *function, const char *fmt, ...);
31 #define erofs_info(sb, fmt, ...) \
32 _erofs_info(sb, __func__, fmt "\n", ##__VA_ARGS__)
33 #ifdef CONFIG_EROFS_FS_DEBUG
34 #define erofs_dbg(x, ...) pr_debug(x "\n", ##__VA_ARGS__)
35 #define DBG_BUGON BUG_ON
37 #define erofs_dbg(x, ...) ((void)0)
38 #define DBG_BUGON(x) ((void)(x))
39 #endif /* !CONFIG_EROFS_FS_DEBUG */
41 /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
42 #define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
44 typedef u64 erofs_nid_t;
45 typedef u64 erofs_off_t;
46 /* data type for filesystem-wide blocks number */
47 typedef u32 erofs_blk_t;
49 struct erofs_fs_context {
50 #ifdef CONFIG_EROFS_FS_ZIP
51 /* current strategy of how to use managed cache */
52 unsigned char cache_strategy;
53 /* strategy of sync decompression (false - auto, true - force on) */
54 bool readahead_sync_decompress;
56 /* threshold for decompression synchronously */
57 unsigned int max_sync_decompress_pages;
59 unsigned int mount_opt;
62 /* all filesystem-wide lz4 configurations */
63 struct erofs_sb_lz4_info {
64 /* # of pages needed for EROFS lz4 rolling decompression */
65 u16 max_distance_pages;
66 /* maximum possible blocks for pclusters in the filesystem */
70 struct erofs_sb_info {
71 #ifdef CONFIG_EROFS_FS_ZIP
72 /* list for all registered superblocks, mainly for shrinker */
73 struct list_head list;
74 struct mutex umount_mutex;
76 /* managed XArray arranged in physical block number */
77 struct xarray managed_pslots;
79 unsigned int shrinker_run_no;
80 u16 available_compr_algs;
82 /* pseudo inode to manage cached pages */
83 struct inode *managed_cache;
85 struct erofs_sb_lz4_info lz4;
86 #endif /* CONFIG_EROFS_FS_ZIP */
89 #ifdef CONFIG_EROFS_FS_XATTR
93 /* inode slot unit size in bit shift */
94 unsigned char islotbits;
96 u32 sb_size; /* total superblock size */
100 /* what we really care is nid, rather than ino.. */
101 erofs_nid_t root_nid;
102 /* used for statfs, f_files - f_favail */
105 u8 uuid[16]; /* 128-bit uuid for volume */
106 u8 volume_name[16]; /* volume name */
108 u32 feature_incompat;
110 struct erofs_fs_context ctx; /* options */
113 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
114 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
116 /* Mount flags set via mount options or defaults */
117 #define EROFS_MOUNT_XATTR_USER 0x00000010
118 #define EROFS_MOUNT_POSIX_ACL 0x00000020
120 #define clear_opt(ctx, option) ((ctx)->mount_opt &= ~EROFS_MOUNT_##option)
121 #define set_opt(ctx, option) ((ctx)->mount_opt |= EROFS_MOUNT_##option)
122 #define test_opt(ctx, option) ((ctx)->mount_opt & EROFS_MOUNT_##option)
125 EROFS_ZIP_CACHE_DISABLED,
126 EROFS_ZIP_CACHE_READAHEAD,
127 EROFS_ZIP_CACHE_READAROUND
130 #ifdef CONFIG_EROFS_FS_ZIP
131 #define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL)
133 /* basic unit of the workstation of a super_block */
134 struct erofs_workgroup {
135 /* the workgroup index in the workstation */
138 /* overall workgroup reference count */
142 #if defined(CONFIG_SMP)
143 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
147 if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
154 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
158 * other observers should notice all modifications
159 * in the freezing period.
162 atomic_set(&grp->refcount, orig_val);
166 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
168 return atomic_cond_read_relaxed(&grp->refcount,
169 VAL != EROFS_LOCKED_MAGIC);
172 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
176 /* no need to spin on UP platforms, let's just disable preemption. */
177 if (val != atomic_read(&grp->refcount)) {
184 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
190 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
192 int v = atomic_read(&grp->refcount);
194 /* workgroup is never freezed on uniprocessor systems */
195 DBG_BUGON(v == EROFS_LOCKED_MAGIC);
198 #endif /* !CONFIG_SMP */
199 #endif /* !CONFIG_EROFS_FS_ZIP */
201 /* we strictly follow PAGE_SIZE and no buffer head yet */
202 #define LOG_BLOCK_SIZE PAGE_SHIFT
204 #undef LOG_SECTORS_PER_BLOCK
205 #define LOG_SECTORS_PER_BLOCK (PAGE_SHIFT - 9)
207 #undef SECTORS_PER_BLOCK
208 #define SECTORS_PER_BLOCK (1 << SECTORS_PER_BLOCK)
210 #define EROFS_BLKSIZ (1 << LOG_BLOCK_SIZE)
212 #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
213 #error erofs cannot be used in this platform
216 #define ROOT_NID(sb) ((sb)->root_nid)
218 #define erofs_blknr(addr) ((addr) / EROFS_BLKSIZ)
219 #define erofs_blkoff(addr) ((addr) % EROFS_BLKSIZ)
220 #define blknr_to_addr(nr) ((erofs_off_t)(nr) * EROFS_BLKSIZ)
222 static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
224 return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
227 #define EROFS_FEATURE_FUNCS(name, compat, feature) \
228 static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
230 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
233 EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING)
234 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
235 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
236 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
238 /* atomic flag definitions */
239 #define EROFS_I_EA_INITED_BIT 0
240 #define EROFS_I_Z_INITED_BIT 1
242 /* bitlock definitions (arranged in reverse order) */
243 #define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
244 #define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
249 /* atomic flags (including bitlocks) */
252 unsigned char datalayout;
253 unsigned char inode_isize;
254 unsigned short xattr_isize;
256 unsigned int xattr_shared_count;
257 unsigned int *xattr_shared_xattrs;
260 erofs_blk_t raw_blkaddr;
261 #ifdef CONFIG_EROFS_FS_ZIP
263 unsigned short z_advise;
264 unsigned char z_algorithmtype[2];
265 unsigned char z_logical_clusterbits;
267 #endif /* CONFIG_EROFS_FS_ZIP */
269 /* the corresponding vfs inode */
270 struct inode vfs_inode;
273 #define EROFS_I(ptr) \
274 container_of(ptr, struct erofs_inode, vfs_inode)
276 static inline unsigned long erofs_inode_datablocks(struct inode *inode)
278 /* since i_size cannot be changed */
279 return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
282 static inline unsigned int erofs_bitrange(unsigned int value, unsigned int bit,
286 return (value >> bit) & ((1 << bits) - 1);
290 static inline unsigned int erofs_inode_version(unsigned int value)
292 return erofs_bitrange(value, EROFS_I_VERSION_BIT,
293 EROFS_I_VERSION_BITS);
296 static inline unsigned int erofs_inode_datalayout(unsigned int value)
298 return erofs_bitrange(value, EROFS_I_DATALAYOUT_BIT,
299 EROFS_I_DATALAYOUT_BITS);
302 extern const struct super_operations erofs_sops;
304 extern const struct address_space_operations erofs_raw_access_aops;
305 extern const struct address_space_operations z_erofs_aops;
308 * Logical to physical block mapping
310 * Different with other file systems, it is used for 2 access modes:
312 * 1) RAW access mode:
314 * Users pass a valid (m_lblk, m_lofs -- usually 0) pair,
315 * and get the valid m_pblk, m_pofs and the longest m_len(in bytes).
317 * Note that m_lblk in the RAW access mode refers to the number of
318 * the compressed ondisk block rather than the uncompressed
319 * in-memory block for the compressed file.
321 * m_pofs equals to m_lofs except for the inline data page.
323 * 2) Normal access mode:
325 * If the inode is not compressed, it has no difference with
326 * the RAW access mode. However, if the inode is compressed,
327 * users should pass a valid (m_lblk, m_lofs) pair, and get
328 * the needed m_pblk, m_pofs, m_len to get the compressed data
329 * and the updated m_lblk, m_lofs which indicates the start
330 * of the corresponding uncompressed data in the file.
333 BH_Zipped = BH_PrivateStart,
337 /* Has a disk mapping */
338 #define EROFS_MAP_MAPPED (1 << BH_Mapped)
339 /* Located in metadata (could be copied from bd_inode) */
340 #define EROFS_MAP_META (1 << BH_Meta)
341 /* The extent has been compressed */
342 #define EROFS_MAP_ZIPPED (1 << BH_Zipped)
343 /* The length of extent is full */
344 #define EROFS_MAP_FULL_MAPPED (1 << BH_FullMapped)
346 struct erofs_map_blocks {
347 erofs_off_t m_pa, m_la;
350 unsigned int m_flags;
355 /* Flags used by erofs_map_blocks_flatmode() */
356 #define EROFS_GET_BLOCKS_RAW 0x0001
359 #ifdef CONFIG_EROFS_FS_ZIP
360 int z_erofs_fill_inode(struct inode *inode);
361 int z_erofs_map_blocks_iter(struct inode *inode,
362 struct erofs_map_blocks *map,
365 static inline int z_erofs_fill_inode(struct inode *inode) { return -EOPNOTSUPP; }
366 static inline int z_erofs_map_blocks_iter(struct inode *inode,
367 struct erofs_map_blocks *map,
372 #endif /* !CONFIG_EROFS_FS_ZIP */
375 struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr);
378 static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
380 #if BITS_PER_LONG == 32
381 return (nid >> 32) ^ (nid & 0xffffffff);
387 extern const struct inode_operations erofs_generic_iops;
388 extern const struct inode_operations erofs_symlink_iops;
389 extern const struct inode_operations erofs_fast_symlink_iops;
391 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir);
392 int erofs_getattr(struct user_namespace *mnt_userns, const struct path *path,
393 struct kstat *stat, u32 request_mask,
394 unsigned int query_flags);
397 extern const struct inode_operations erofs_dir_iops;
399 int erofs_namei(struct inode *dir, struct qstr *name,
400 erofs_nid_t *nid, unsigned int *d_type);
403 extern const struct file_operations erofs_dir_fops;
405 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
410 void *p = vm_map_ram(pages, count, -1);
412 /* retry two more times (totally 3 times) */
413 if (p || ++retried >= 3)
421 void *erofs_get_pcpubuf(unsigned int requiredpages);
422 void erofs_put_pcpubuf(void *ptr);
423 int erofs_pcpubuf_growsize(unsigned int nrpages);
424 void erofs_pcpubuf_init(void);
425 void erofs_pcpubuf_exit(void);
427 /* utils.c / zdata.c */
428 struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp);
430 #ifdef CONFIG_EROFS_FS_ZIP
431 int erofs_workgroup_put(struct erofs_workgroup *grp);
432 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
434 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
435 struct erofs_workgroup *grp);
436 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
437 void erofs_shrinker_register(struct super_block *sb);
438 void erofs_shrinker_unregister(struct super_block *sb);
439 int __init erofs_init_shrinker(void);
440 void erofs_exit_shrinker(void);
441 int __init z_erofs_init_zip_subsystem(void);
442 void z_erofs_exit_zip_subsystem(void);
443 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
444 struct erofs_workgroup *egrp);
445 int erofs_try_to_free_cached_page(struct address_space *mapping,
447 int z_erofs_load_lz4_config(struct super_block *sb,
448 struct erofs_super_block *dsb,
449 struct z_erofs_lz4_cfgs *lz4, int len);
451 static inline void erofs_shrinker_register(struct super_block *sb) {}
452 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
453 static inline int erofs_init_shrinker(void) { return 0; }
454 static inline void erofs_exit_shrinker(void) {}
455 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
456 static inline void z_erofs_exit_zip_subsystem(void) {}
457 static inline int z_erofs_load_lz4_config(struct super_block *sb,
458 struct erofs_super_block *dsb,
459 struct z_erofs_lz4_cfgs *lz4, int len)
461 if (lz4 || dsb->u1.lz4_max_distance) {
462 erofs_err(sb, "lz4 algorithm isn't enabled");
467 #endif /* !CONFIG_EROFS_FS_ZIP */
469 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
471 #endif /* __EROFS_INTERNAL_H */