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/dax.h>
12 #include <linux/dcache.h>
14 #include <linux/module.h>
15 #include <linux/pagemap.h>
16 #include <linux/bio.h>
17 #include <linux/magic.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/iomap.h>
23 /* redefine pr_fmt "erofs: " */
25 #define pr_fmt(fmt) "erofs: " fmt
27 __printf(3, 4) void _erofs_err(struct super_block *sb,
28 const char *function, const char *fmt, ...);
29 #define erofs_err(sb, fmt, ...) \
30 _erofs_err(sb, __func__, fmt "\n", ##__VA_ARGS__)
31 __printf(3, 4) void _erofs_info(struct super_block *sb,
32 const char *function, const char *fmt, ...);
33 #define erofs_info(sb, fmt, ...) \
34 _erofs_info(sb, __func__, fmt "\n", ##__VA_ARGS__)
35 #ifdef CONFIG_EROFS_FS_DEBUG
36 #define DBG_BUGON BUG_ON
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_device_info {
51 struct erofs_fscache *fscache;
52 struct file *bdev_file;
53 struct dax_device *dax_dev;
61 EROFS_SYNC_DECOMPRESS_AUTO,
62 EROFS_SYNC_DECOMPRESS_FORCE_ON,
63 EROFS_SYNC_DECOMPRESS_FORCE_OFF
66 struct erofs_mount_opts {
67 #ifdef CONFIG_EROFS_FS_ZIP
68 /* current strategy of how to use managed cache */
69 unsigned char cache_strategy;
70 /* strategy of sync decompression (0 - auto, 1 - force on, 2 - force off) */
71 unsigned int sync_decompress;
73 /* threshold for decompression synchronously */
74 unsigned int max_sync_decompress_pages;
76 unsigned int mount_opt;
79 struct erofs_dev_context {
81 struct rw_semaphore rwsem;
83 unsigned int extra_devices;
87 /* all filesystem-wide lz4 configurations */
88 struct erofs_sb_lz4_info {
89 /* # of pages needed for EROFS lz4 rolling decompression */
90 u16 max_distance_pages;
91 /* maximum possible blocks for pclusters in the filesystem */
97 struct list_head list;
98 struct fscache_volume *volume;
102 struct erofs_fscache {
103 struct fscache_cookie *cookie;
104 struct inode *inode; /* anonymous inode for the blob */
106 /* used for share domain mode */
107 struct erofs_domain *domain;
108 struct list_head node;
113 struct erofs_xattr_prefix_item {
114 struct erofs_xattr_long_prefix *prefix;
118 struct erofs_sb_info {
119 struct erofs_mount_opts opt; /* options */
120 #ifdef CONFIG_EROFS_FS_ZIP
121 /* list for all registered superblocks, mainly for shrinker */
122 struct list_head list;
123 struct mutex umount_mutex;
125 /* managed XArray arranged in physical block number */
126 struct xarray managed_pslots;
128 unsigned int shrinker_run_no;
129 u16 available_compr_algs;
131 /* pseudo inode to manage cached pages */
132 struct inode *managed_cache;
134 struct erofs_sb_lz4_info lz4;
135 #endif /* CONFIG_EROFS_FS_ZIP */
136 struct inode *packed_inode;
137 struct erofs_dev_context *devs;
138 struct dax_device *dax_dev;
141 u32 primarydevice_blocks;
144 #ifdef CONFIG_EROFS_FS_XATTR
146 u32 xattr_prefix_start;
147 u8 xattr_prefix_count;
148 struct erofs_xattr_prefix_item *xattr_prefixes;
149 unsigned int xattr_filter_reserved;
151 u16 device_id_mask; /* valid bits of device id to be used */
153 unsigned char islotbits; /* inode slot unit size in bit shift */
154 unsigned char blkszbits; /* filesystem block size in bit shift */
156 u32 sb_size; /* total superblock size */
160 /* what we really care is nid, rather than ino.. */
161 erofs_nid_t root_nid;
162 erofs_nid_t packed_nid;
163 /* used for statfs, f_files - f_favail */
166 u8 uuid[16]; /* 128-bit uuid for volume */
167 u8 volume_name[16]; /* volume name */
169 u32 feature_incompat;
172 struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
173 struct completion s_kobj_unregister;
175 /* fscache support */
176 struct fscache_volume *volume;
177 struct erofs_fscache *s_fscache;
178 struct erofs_domain *domain;
183 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
184 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
186 /* Mount flags set via mount options or defaults */
187 #define EROFS_MOUNT_XATTR_USER 0x00000010
188 #define EROFS_MOUNT_POSIX_ACL 0x00000020
189 #define EROFS_MOUNT_DAX_ALWAYS 0x00000040
190 #define EROFS_MOUNT_DAX_NEVER 0x00000080
192 #define clear_opt(opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)
193 #define set_opt(opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)
194 #define test_opt(opt, option) ((opt)->mount_opt & EROFS_MOUNT_##option)
196 static inline bool erofs_is_fscache_mode(struct super_block *sb)
198 return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && !sb->s_bdev;
202 EROFS_ZIP_CACHE_DISABLED,
203 EROFS_ZIP_CACHE_READAHEAD,
204 EROFS_ZIP_CACHE_READAROUND
207 /* basic unit of the workstation of a super_block */
208 struct erofs_workgroup {
210 struct lockref lockref;
213 enum erofs_kmap_type {
214 EROFS_NO_KMAP, /* don't map the buffer */
215 EROFS_KMAP, /* use kmap_local_page() to map the buffer */
222 enum erofs_kmap_type kmap_type;
224 #define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL })
226 #define erofs_blknr(sb, addr) ((addr) >> (sb)->s_blocksize_bits)
227 #define erofs_blkoff(sb, addr) ((addr) & ((sb)->s_blocksize - 1))
228 #define erofs_pos(sb, blk) ((erofs_off_t)(blk) << (sb)->s_blocksize_bits)
229 #define erofs_iblks(i) (round_up((i)->i_size, i_blocksize(i)) >> (i)->i_blkbits)
231 #define EROFS_FEATURE_FUNCS(name, compat, feature) \
232 static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
234 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
237 EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
238 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
239 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
240 EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
241 EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
242 EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
243 EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
244 EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
245 EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
246 EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
247 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
248 EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
250 /* atomic flag definitions */
251 #define EROFS_I_EA_INITED_BIT 0
252 #define EROFS_I_Z_INITED_BIT 1
254 /* bitlock definitions (arranged in reverse order) */
255 #define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
256 #define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
261 /* atomic flags (including bitlocks) */
264 unsigned char datalayout;
265 unsigned char inode_isize;
266 unsigned int xattr_isize;
268 unsigned int xattr_name_filter;
269 unsigned int xattr_shared_count;
270 unsigned int *xattr_shared_xattrs;
273 erofs_blk_t raw_blkaddr;
275 unsigned short chunkformat;
276 unsigned char chunkbits;
278 #ifdef CONFIG_EROFS_FS_ZIP
280 unsigned short z_advise;
281 unsigned char z_algorithmtype[2];
282 unsigned char z_logical_clusterbits;
283 unsigned long z_tailextent_headlcn;
286 erofs_off_t z_idataoff;
287 unsigned short z_idata_size;
289 erofs_off_t z_fragmentoff;
292 #endif /* CONFIG_EROFS_FS_ZIP */
294 /* the corresponding vfs inode */
295 struct inode vfs_inode;
298 #define EROFS_I(ptr) container_of(ptr, struct erofs_inode, vfs_inode)
300 static inline erofs_off_t erofs_iloc(struct inode *inode)
302 struct erofs_sb_info *sbi = EROFS_I_SB(inode);
304 return erofs_pos(inode->i_sb, sbi->meta_blkaddr) +
305 (EROFS_I(inode)->nid << sbi->islotbits);
308 static inline unsigned int erofs_inode_version(unsigned int ifmt)
310 return (ifmt >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASK;
313 static inline unsigned int erofs_inode_datalayout(unsigned int ifmt)
315 return (ifmt >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASK;
319 * Different from grab_cache_page_nowait(), reclaiming is never triggered
320 * when allocating new pages.
323 struct page *erofs_grab_cache_page_nowait(struct address_space *mapping,
326 return pagecache_get_page(mapping, index,
327 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
328 readahead_gfp_mask(mapping) & ~__GFP_RECLAIM);
331 /* Has a disk mapping */
332 #define EROFS_MAP_MAPPED 0x0001
333 /* Located in metadata (could be copied from bd_inode) */
334 #define EROFS_MAP_META 0x0002
335 /* The extent is encoded */
336 #define EROFS_MAP_ENCODED 0x0004
337 /* The length of extent is full */
338 #define EROFS_MAP_FULL_MAPPED 0x0008
339 /* Located in the special packed inode */
340 #define EROFS_MAP_FRAGMENT 0x0010
341 /* The extent refers to partial decompressed data */
342 #define EROFS_MAP_PARTIAL_REF 0x0020
344 struct erofs_map_blocks {
345 struct erofs_buf buf;
347 erofs_off_t m_pa, m_la;
350 unsigned short m_deviceid;
351 char m_algorithmformat;
352 unsigned int m_flags;
356 * Used to get the exact decompressed length, e.g. fiemap (consider lookback
357 * approach instead if possible since it's more metadata lightweight.)
359 #define EROFS_GET_BLOCKS_FIEMAP 0x0001
360 /* Used to map the whole extent if non-negligible data is requested for LZMA */
361 #define EROFS_GET_BLOCKS_READMORE 0x0002
362 /* Used to map tail extent for tailpacking inline or fragment pcluster */
363 #define EROFS_GET_BLOCKS_FINDTAIL 0x0004
366 Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
367 Z_EROFS_COMPRESSION_INTERLACED,
368 Z_EROFS_COMPRESSION_RUNTIME_MAX
371 struct erofs_map_dev {
372 struct erofs_fscache *m_fscache;
373 struct block_device *m_bdev;
374 struct dax_device *m_daxdev;
378 unsigned int m_deviceid;
381 extern const struct super_operations erofs_sops;
383 extern const struct address_space_operations erofs_raw_access_aops;
384 extern const struct address_space_operations z_erofs_aops;
385 extern const struct address_space_operations erofs_fscache_access_aops;
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;
390 extern const struct inode_operations erofs_dir_iops;
392 extern const struct file_operations erofs_file_fops;
393 extern const struct file_operations erofs_dir_fops;
395 extern const struct iomap_ops z_erofs_iomap_report_ops;
397 /* flags for erofs_fscache_register_cookie() */
398 #define EROFS_REG_COOKIE_SHARE 0x0001
399 #define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002
401 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
402 erofs_off_t *offset, int *lengthp);
403 void erofs_unmap_metabuf(struct erofs_buf *buf);
404 void erofs_put_metabuf(struct erofs_buf *buf);
405 void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
406 enum erofs_kmap_type type);
407 void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
408 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
409 erofs_blk_t blkaddr, enum erofs_kmap_type type);
410 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
411 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
413 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
414 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
415 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
416 struct kstat *stat, u32 request_mask,
417 unsigned int query_flags);
418 int erofs_namei(struct inode *dir, const struct qstr *name,
419 erofs_nid_t *nid, unsigned int *d_type);
421 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
426 void *p = vm_map_ram(pages, count, -1);
428 /* retry two more times (totally 3 times) */
429 if (p || ++retried >= 3)
436 int erofs_register_sysfs(struct super_block *sb);
437 void erofs_unregister_sysfs(struct super_block *sb);
438 int __init erofs_init_sysfs(void);
439 void erofs_exit_sysfs(void);
441 struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
442 static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
444 set_page_private(page, (unsigned long)*pagepool);
447 void erofs_release_pages(struct page **pagepool);
449 #ifdef CONFIG_EROFS_FS_ZIP
450 void erofs_workgroup_put(struct erofs_workgroup *grp);
451 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
453 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
454 struct erofs_workgroup *grp);
455 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
456 void erofs_shrinker_register(struct super_block *sb);
457 void erofs_shrinker_unregister(struct super_block *sb);
458 int __init erofs_init_shrinker(void);
459 void erofs_exit_shrinker(void);
460 int __init z_erofs_init_zip_subsystem(void);
461 void z_erofs_exit_zip_subsystem(void);
462 int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,
463 struct erofs_workgroup *egrp);
464 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
466 void *erofs_get_pcpubuf(unsigned int requiredpages);
467 void erofs_put_pcpubuf(void *ptr);
468 int erofs_pcpubuf_growsize(unsigned int nrpages);
469 void __init erofs_pcpubuf_init(void);
470 void erofs_pcpubuf_exit(void);
471 int erofs_init_managed_cache(struct super_block *sb);
472 int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb);
474 static inline void erofs_shrinker_register(struct super_block *sb) {}
475 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
476 static inline int erofs_init_shrinker(void) { return 0; }
477 static inline void erofs_exit_shrinker(void) {}
478 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
479 static inline void z_erofs_exit_zip_subsystem(void) {}
480 static inline void erofs_pcpubuf_init(void) {}
481 static inline void erofs_pcpubuf_exit(void) {}
482 static inline int erofs_init_managed_cache(struct super_block *sb) { return 0; }
483 #endif /* !CONFIG_EROFS_FS_ZIP */
485 #ifdef CONFIG_EROFS_FS_ZIP_LZMA
486 int __init z_erofs_lzma_init(void);
487 void z_erofs_lzma_exit(void);
489 static inline int z_erofs_lzma_init(void) { return 0; }
490 static inline int z_erofs_lzma_exit(void) { return 0; }
491 #endif /* !CONFIG_EROFS_FS_ZIP_LZMA */
493 #ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
494 int __init z_erofs_deflate_init(void);
495 void z_erofs_deflate_exit(void);
497 static inline int z_erofs_deflate_init(void) { return 0; }
498 static inline int z_erofs_deflate_exit(void) { return 0; }
499 #endif /* !CONFIG_EROFS_FS_ZIP_DEFLATE */
501 #ifdef CONFIG_EROFS_FS_ONDEMAND
502 int erofs_fscache_register_fs(struct super_block *sb);
503 void erofs_fscache_unregister_fs(struct super_block *sb);
505 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
506 char *name, unsigned int flags);
507 void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
508 struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev);
509 void erofs_fscache_submit_bio(struct bio *bio);
511 static inline int erofs_fscache_register_fs(struct super_block *sb)
515 static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
518 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
519 char *name, unsigned int flags)
521 return ERR_PTR(-EOPNOTSUPP);
524 static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
527 static inline struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev) { return NULL; }
528 static inline void erofs_fscache_submit_bio(struct bio *bio) {}
531 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
533 #endif /* __EROFS_INTERNAL_H */