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/magic.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/iomap.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 DBG_BUGON BUG_ON
36 #define DBG_BUGON(x) ((void)(x))
37 #endif /* !CONFIG_EROFS_FS_DEBUG */
39 /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
40 #define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
42 typedef u64 erofs_nid_t;
43 typedef u64 erofs_off_t;
44 /* data type for filesystem-wide blocks number */
45 typedef u32 erofs_blk_t;
47 struct erofs_device_info {
49 struct erofs_fscache *fscache;
50 struct block_device *bdev;
51 struct dax_device *dax_dev;
59 EROFS_SYNC_DECOMPRESS_AUTO,
60 EROFS_SYNC_DECOMPRESS_FORCE_ON,
61 EROFS_SYNC_DECOMPRESS_FORCE_OFF
64 struct erofs_mount_opts {
65 #ifdef CONFIG_EROFS_FS_ZIP
66 /* current strategy of how to use managed cache */
67 unsigned char cache_strategy;
68 /* strategy of sync decompression (0 - auto, 1 - force on, 2 - force off) */
69 unsigned int sync_decompress;
71 /* threshold for decompression synchronously */
72 unsigned int max_sync_decompress_pages;
74 unsigned int mount_opt;
77 struct erofs_dev_context {
79 struct rw_semaphore rwsem;
81 unsigned int extra_devices;
85 struct erofs_fs_context {
86 struct erofs_mount_opts opt;
87 struct erofs_dev_context *devs;
92 /* all filesystem-wide lz4 configurations */
93 struct erofs_sb_lz4_info {
94 /* # of pages needed for EROFS lz4 rolling decompression */
95 u16 max_distance_pages;
96 /* maximum possible blocks for pclusters in the filesystem */
100 struct erofs_domain {
102 struct list_head list;
103 struct fscache_volume *volume;
107 struct erofs_fscache {
108 struct fscache_cookie *cookie;
109 struct inode *inode; /* anonymous inode for the blob */
111 /* used for share domain mode */
112 struct erofs_domain *domain;
113 struct list_head node;
118 struct erofs_xattr_prefix_item {
119 struct erofs_xattr_long_prefix *prefix;
123 struct erofs_sb_info {
124 struct erofs_mount_opts opt; /* options */
125 #ifdef CONFIG_EROFS_FS_ZIP
126 /* list for all registered superblocks, mainly for shrinker */
127 struct list_head list;
128 struct mutex umount_mutex;
130 /* managed XArray arranged in physical block number */
131 struct xarray managed_pslots;
133 unsigned int shrinker_run_no;
134 u16 available_compr_algs;
136 /* pseudo inode to manage cached pages */
137 struct inode *managed_cache;
139 struct erofs_sb_lz4_info lz4;
140 #endif /* CONFIG_EROFS_FS_ZIP */
141 struct inode *packed_inode;
142 struct erofs_dev_context *devs;
143 struct dax_device *dax_dev;
146 u32 primarydevice_blocks;
149 #ifdef CONFIG_EROFS_FS_XATTR
151 u32 xattr_prefix_start;
152 u8 xattr_prefix_count;
153 struct erofs_xattr_prefix_item *xattr_prefixes;
155 u16 device_id_mask; /* valid bits of device id to be used */
157 unsigned char islotbits; /* inode slot unit size in bit shift */
158 unsigned char blkszbits; /* filesystem block size in bit shift */
160 u32 sb_size; /* total superblock size */
164 /* what we really care is nid, rather than ino.. */
165 erofs_nid_t root_nid;
166 erofs_nid_t packed_nid;
167 /* used for statfs, f_files - f_favail */
170 u8 uuid[16]; /* 128-bit uuid for volume */
171 u8 volume_name[16]; /* volume name */
173 u32 feature_incompat;
176 struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
177 struct completion s_kobj_unregister;
179 /* fscache support */
180 struct fscache_volume *volume;
181 struct erofs_fscache *s_fscache;
182 struct erofs_domain *domain;
187 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
188 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
190 /* Mount flags set via mount options or defaults */
191 #define EROFS_MOUNT_XATTR_USER 0x00000010
192 #define EROFS_MOUNT_POSIX_ACL 0x00000020
193 #define EROFS_MOUNT_DAX_ALWAYS 0x00000040
194 #define EROFS_MOUNT_DAX_NEVER 0x00000080
196 #define clear_opt(opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)
197 #define set_opt(opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)
198 #define test_opt(opt, option) ((opt)->mount_opt & EROFS_MOUNT_##option)
200 static inline bool erofs_is_fscache_mode(struct super_block *sb)
202 return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && !sb->s_bdev;
206 EROFS_ZIP_CACHE_DISABLED,
207 EROFS_ZIP_CACHE_READAHEAD,
208 EROFS_ZIP_CACHE_READAROUND
211 #define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL)
213 /* basic unit of the workstation of a super_block */
214 struct erofs_workgroup {
215 /* the workgroup index in the workstation */
218 /* overall workgroup reference count */
222 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
226 if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
233 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
237 * other observers should notice all modifications
238 * in the freezing period.
241 atomic_set(&grp->refcount, orig_val);
245 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
247 return atomic_cond_read_relaxed(&grp->refcount,
248 VAL != EROFS_LOCKED_MAGIC);
251 enum erofs_kmap_type {
252 EROFS_NO_KMAP, /* don't map the buffer */
253 EROFS_KMAP, /* use kmap_local_page() to map the buffer */
260 enum erofs_kmap_type kmap_type;
262 #define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL })
264 #define ROOT_NID(sb) ((sb)->root_nid)
266 #define erofs_blknr(sb, addr) ((addr) >> (sb)->s_blocksize_bits)
267 #define erofs_blkoff(sb, addr) ((addr) & ((sb)->s_blocksize - 1))
268 #define erofs_pos(sb, blk) ((erofs_off_t)(blk) << (sb)->s_blocksize_bits)
269 #define erofs_iblks(i) (round_up((i)->i_size, i_blocksize(i)) >> (i)->i_blkbits)
271 #define EROFS_FEATURE_FUNCS(name, compat, feature) \
272 static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
274 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
277 EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
278 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
279 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
280 EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
281 EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
282 EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
283 EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
284 EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
285 EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
286 EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
287 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
289 /* atomic flag definitions */
290 #define EROFS_I_EA_INITED_BIT 0
291 #define EROFS_I_Z_INITED_BIT 1
293 /* bitlock definitions (arranged in reverse order) */
294 #define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
295 #define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
300 /* atomic flags (including bitlocks) */
303 unsigned char datalayout;
304 unsigned char inode_isize;
305 unsigned int xattr_isize;
307 unsigned int xattr_shared_count;
308 unsigned int *xattr_shared_xattrs;
311 erofs_blk_t raw_blkaddr;
313 unsigned short chunkformat;
314 unsigned char chunkbits;
316 #ifdef CONFIG_EROFS_FS_ZIP
318 unsigned short z_advise;
319 unsigned char z_algorithmtype[2];
320 unsigned char z_logical_clusterbits;
321 unsigned long z_tailextent_headlcn;
324 erofs_off_t z_idataoff;
325 unsigned short z_idata_size;
327 erofs_off_t z_fragmentoff;
330 #endif /* CONFIG_EROFS_FS_ZIP */
332 /* the corresponding vfs inode */
333 struct inode vfs_inode;
336 #define EROFS_I(ptr) container_of(ptr, struct erofs_inode, vfs_inode)
338 static inline erofs_off_t erofs_iloc(struct inode *inode)
340 struct erofs_sb_info *sbi = EROFS_I_SB(inode);
342 return erofs_pos(inode->i_sb, sbi->meta_blkaddr) +
343 (EROFS_I(inode)->nid << sbi->islotbits);
346 static inline unsigned int erofs_inode_version(unsigned int ifmt)
348 return (ifmt >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASK;
351 static inline unsigned int erofs_inode_datalayout(unsigned int ifmt)
353 return (ifmt >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASK;
357 * Different from grab_cache_page_nowait(), reclaiming is never triggered
358 * when allocating new pages.
361 struct page *erofs_grab_cache_page_nowait(struct address_space *mapping,
364 return pagecache_get_page(mapping, index,
365 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
366 readahead_gfp_mask(mapping) & ~__GFP_RECLAIM);
369 /* Has a disk mapping */
370 #define EROFS_MAP_MAPPED 0x0001
371 /* Located in metadata (could be copied from bd_inode) */
372 #define EROFS_MAP_META 0x0002
373 /* The extent is encoded */
374 #define EROFS_MAP_ENCODED 0x0004
375 /* The length of extent is full */
376 #define EROFS_MAP_FULL_MAPPED 0x0008
377 /* Located in the special packed inode */
378 #define EROFS_MAP_FRAGMENT 0x0010
379 /* The extent refers to partial decompressed data */
380 #define EROFS_MAP_PARTIAL_REF 0x0020
382 struct erofs_map_blocks {
383 struct erofs_buf buf;
385 erofs_off_t m_pa, m_la;
388 unsigned short m_deviceid;
389 char m_algorithmformat;
390 unsigned int m_flags;
394 * Used to get the exact decompressed length, e.g. fiemap (consider lookback
395 * approach instead if possible since it's more metadata lightweight.)
397 #define EROFS_GET_BLOCKS_FIEMAP 0x0001
398 /* Used to map the whole extent if non-negligible data is requested for LZMA */
399 #define EROFS_GET_BLOCKS_READMORE 0x0002
400 /* Used to map tail extent for tailpacking inline or fragment pcluster */
401 #define EROFS_GET_BLOCKS_FINDTAIL 0x0004
404 Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
405 Z_EROFS_COMPRESSION_INTERLACED,
406 Z_EROFS_COMPRESSION_RUNTIME_MAX
409 struct erofs_map_dev {
410 struct erofs_fscache *m_fscache;
411 struct block_device *m_bdev;
412 struct dax_device *m_daxdev;
416 unsigned int m_deviceid;
419 extern struct file_system_type erofs_fs_type;
420 extern const struct super_operations erofs_sops;
422 extern const struct address_space_operations erofs_raw_access_aops;
423 extern const struct address_space_operations z_erofs_aops;
424 extern const struct address_space_operations erofs_fscache_access_aops;
426 extern const struct inode_operations erofs_generic_iops;
427 extern const struct inode_operations erofs_symlink_iops;
428 extern const struct inode_operations erofs_fast_symlink_iops;
429 extern const struct inode_operations erofs_dir_iops;
431 extern const struct file_operations erofs_file_fops;
432 extern const struct file_operations erofs_dir_fops;
434 extern const struct iomap_ops z_erofs_iomap_report_ops;
436 /* flags for erofs_fscache_register_cookie() */
437 #define EROFS_REG_COOKIE_SHARE 0x0001
438 #define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002
440 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
441 erofs_off_t *offset, int *lengthp);
442 void erofs_unmap_metabuf(struct erofs_buf *buf);
443 void erofs_put_metabuf(struct erofs_buf *buf);
444 void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
445 enum erofs_kmap_type type);
446 void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
447 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
448 erofs_blk_t blkaddr, enum erofs_kmap_type type);
449 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
450 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
452 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
453 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
454 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
455 struct kstat *stat, u32 request_mask,
456 unsigned int query_flags);
457 int erofs_namei(struct inode *dir, const struct qstr *name,
458 erofs_nid_t *nid, unsigned int *d_type);
460 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
465 void *p = vm_map_ram(pages, count, -1);
467 /* retry two more times (totally 3 times) */
468 if (p || ++retried >= 3)
475 int erofs_register_sysfs(struct super_block *sb);
476 void erofs_unregister_sysfs(struct super_block *sb);
477 int __init erofs_init_sysfs(void);
478 void erofs_exit_sysfs(void);
480 struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
481 static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
483 set_page_private(page, (unsigned long)*pagepool);
486 void erofs_release_pages(struct page **pagepool);
488 #ifdef CONFIG_EROFS_FS_ZIP
489 int erofs_workgroup_put(struct erofs_workgroup *grp);
490 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
492 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
493 struct erofs_workgroup *grp);
494 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
495 void erofs_shrinker_register(struct super_block *sb);
496 void erofs_shrinker_unregister(struct super_block *sb);
497 int __init erofs_init_shrinker(void);
498 void erofs_exit_shrinker(void);
499 int __init z_erofs_init_zip_subsystem(void);
500 void z_erofs_exit_zip_subsystem(void);
501 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
502 struct erofs_workgroup *egrp);
503 int erofs_try_to_free_cached_page(struct page *page);
504 int z_erofs_load_lz4_config(struct super_block *sb,
505 struct erofs_super_block *dsb,
506 struct z_erofs_lz4_cfgs *lz4, int len);
507 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
509 void *erofs_get_pcpubuf(unsigned int requiredpages);
510 void erofs_put_pcpubuf(void *ptr);
511 int erofs_pcpubuf_growsize(unsigned int nrpages);
512 void __init erofs_pcpubuf_init(void);
513 void erofs_pcpubuf_exit(void);
515 static inline void erofs_shrinker_register(struct super_block *sb) {}
516 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
517 static inline int erofs_init_shrinker(void) { return 0; }
518 static inline void erofs_exit_shrinker(void) {}
519 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
520 static inline void z_erofs_exit_zip_subsystem(void) {}
521 static inline int z_erofs_load_lz4_config(struct super_block *sb,
522 struct erofs_super_block *dsb,
523 struct z_erofs_lz4_cfgs *lz4, int len)
525 if (lz4 || dsb->u1.lz4_max_distance) {
526 erofs_err(sb, "lz4 algorithm isn't enabled");
531 static inline void erofs_pcpubuf_init(void) {}
532 static inline void erofs_pcpubuf_exit(void) {}
533 #endif /* !CONFIG_EROFS_FS_ZIP */
535 #ifdef CONFIG_EROFS_FS_ZIP_LZMA
536 int __init z_erofs_lzma_init(void);
537 void z_erofs_lzma_exit(void);
538 int z_erofs_load_lzma_config(struct super_block *sb,
539 struct erofs_super_block *dsb,
540 struct z_erofs_lzma_cfgs *lzma, int size);
542 static inline int z_erofs_lzma_init(void) { return 0; }
543 static inline int z_erofs_lzma_exit(void) { return 0; }
544 static inline int z_erofs_load_lzma_config(struct super_block *sb,
545 struct erofs_super_block *dsb,
546 struct z_erofs_lzma_cfgs *lzma, int size) {
548 erofs_err(sb, "lzma algorithm isn't enabled");
553 #endif /* !CONFIG_EROFS_FS_ZIP_LZMA */
555 #ifdef CONFIG_EROFS_FS_ONDEMAND
556 int erofs_fscache_register_fs(struct super_block *sb);
557 void erofs_fscache_unregister_fs(struct super_block *sb);
559 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
560 char *name, unsigned int flags);
561 void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
563 static inline int erofs_fscache_register_fs(struct super_block *sb)
567 static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
570 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
571 char *name, unsigned int flags)
573 return ERR_PTR(-EOPNOTSUPP);
576 static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
581 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
583 #endif /* __EROFS_INTERNAL_H */