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 bdev_handle *bdev_handle;
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 struct erofs_fs_context {
88 struct erofs_mount_opts opt;
89 struct erofs_dev_context *devs;
94 /* all filesystem-wide lz4 configurations */
95 struct erofs_sb_lz4_info {
96 /* # of pages needed for EROFS lz4 rolling decompression */
97 u16 max_distance_pages;
98 /* maximum possible blocks for pclusters in the filesystem */
102 struct erofs_domain {
104 struct list_head list;
105 struct fscache_volume *volume;
109 struct erofs_fscache {
110 struct fscache_cookie *cookie;
111 struct inode *inode; /* anonymous inode for the blob */
113 /* used for share domain mode */
114 struct erofs_domain *domain;
115 struct list_head node;
120 struct erofs_xattr_prefix_item {
121 struct erofs_xattr_long_prefix *prefix;
125 struct erofs_sb_info {
126 struct erofs_mount_opts opt; /* options */
127 #ifdef CONFIG_EROFS_FS_ZIP
128 /* list for all registered superblocks, mainly for shrinker */
129 struct list_head list;
130 struct mutex umount_mutex;
132 /* managed XArray arranged in physical block number */
133 struct xarray managed_pslots;
135 unsigned int shrinker_run_no;
136 u16 available_compr_algs;
138 /* pseudo inode to manage cached pages */
139 struct inode *managed_cache;
141 struct erofs_sb_lz4_info lz4;
142 #endif /* CONFIG_EROFS_FS_ZIP */
143 struct inode *packed_inode;
144 struct erofs_dev_context *devs;
145 struct dax_device *dax_dev;
148 u32 primarydevice_blocks;
151 #ifdef CONFIG_EROFS_FS_XATTR
153 u32 xattr_prefix_start;
154 u8 xattr_prefix_count;
155 struct erofs_xattr_prefix_item *xattr_prefixes;
156 unsigned int xattr_filter_reserved;
158 u16 device_id_mask; /* valid bits of device id to be used */
160 unsigned char islotbits; /* inode slot unit size in bit shift */
161 unsigned char blkszbits; /* filesystem block size in bit shift */
163 u32 sb_size; /* total superblock size */
167 /* what we really care is nid, rather than ino.. */
168 erofs_nid_t root_nid;
169 erofs_nid_t packed_nid;
170 /* used for statfs, f_files - f_favail */
173 u8 uuid[16]; /* 128-bit uuid for volume */
174 u8 volume_name[16]; /* volume name */
176 u32 feature_incompat;
179 struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
180 struct completion s_kobj_unregister;
182 /* fscache support */
183 struct fscache_volume *volume;
184 struct erofs_fscache *s_fscache;
185 struct erofs_domain *domain;
190 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
191 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
193 /* Mount flags set via mount options or defaults */
194 #define EROFS_MOUNT_XATTR_USER 0x00000010
195 #define EROFS_MOUNT_POSIX_ACL 0x00000020
196 #define EROFS_MOUNT_DAX_ALWAYS 0x00000040
197 #define EROFS_MOUNT_DAX_NEVER 0x00000080
199 #define clear_opt(opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)
200 #define set_opt(opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)
201 #define test_opt(opt, option) ((opt)->mount_opt & EROFS_MOUNT_##option)
203 static inline bool erofs_is_fscache_mode(struct super_block *sb)
205 return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && !sb->s_bdev;
209 EROFS_ZIP_CACHE_DISABLED,
210 EROFS_ZIP_CACHE_READAHEAD,
211 EROFS_ZIP_CACHE_READAROUND
214 /* basic unit of the workstation of a super_block */
215 struct erofs_workgroup {
217 struct lockref lockref;
220 enum erofs_kmap_type {
221 EROFS_NO_KMAP, /* don't map the buffer */
222 EROFS_KMAP, /* use kmap_local_page() to map the buffer */
229 enum erofs_kmap_type kmap_type;
231 #define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL })
233 #define erofs_blknr(sb, addr) ((addr) >> (sb)->s_blocksize_bits)
234 #define erofs_blkoff(sb, addr) ((addr) & ((sb)->s_blocksize - 1))
235 #define erofs_pos(sb, blk) ((erofs_off_t)(blk) << (sb)->s_blocksize_bits)
236 #define erofs_iblks(i) (round_up((i)->i_size, i_blocksize(i)) >> (i)->i_blkbits)
238 #define EROFS_FEATURE_FUNCS(name, compat, feature) \
239 static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
241 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
244 EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
245 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
246 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
247 EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
248 EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
249 EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
250 EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
251 EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
252 EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
253 EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
254 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
255 EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
257 /* atomic flag definitions */
258 #define EROFS_I_EA_INITED_BIT 0
259 #define EROFS_I_Z_INITED_BIT 1
261 /* bitlock definitions (arranged in reverse order) */
262 #define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
263 #define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
268 /* atomic flags (including bitlocks) */
271 unsigned char datalayout;
272 unsigned char inode_isize;
273 unsigned int xattr_isize;
275 unsigned int xattr_name_filter;
276 unsigned int xattr_shared_count;
277 unsigned int *xattr_shared_xattrs;
280 erofs_blk_t raw_blkaddr;
282 unsigned short chunkformat;
283 unsigned char chunkbits;
285 #ifdef CONFIG_EROFS_FS_ZIP
287 unsigned short z_advise;
288 unsigned char z_algorithmtype[2];
289 unsigned char z_logical_clusterbits;
290 unsigned long z_tailextent_headlcn;
293 erofs_off_t z_idataoff;
294 unsigned short z_idata_size;
296 erofs_off_t z_fragmentoff;
299 #endif /* CONFIG_EROFS_FS_ZIP */
301 /* the corresponding vfs inode */
302 struct inode vfs_inode;
305 #define EROFS_I(ptr) container_of(ptr, struct erofs_inode, vfs_inode)
307 static inline erofs_off_t erofs_iloc(struct inode *inode)
309 struct erofs_sb_info *sbi = EROFS_I_SB(inode);
311 return erofs_pos(inode->i_sb, sbi->meta_blkaddr) +
312 (EROFS_I(inode)->nid << sbi->islotbits);
315 static inline unsigned int erofs_inode_version(unsigned int ifmt)
317 return (ifmt >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASK;
320 static inline unsigned int erofs_inode_datalayout(unsigned int ifmt)
322 return (ifmt >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASK;
326 * Different from grab_cache_page_nowait(), reclaiming is never triggered
327 * when allocating new pages.
330 struct page *erofs_grab_cache_page_nowait(struct address_space *mapping,
333 return pagecache_get_page(mapping, index,
334 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
335 readahead_gfp_mask(mapping) & ~__GFP_RECLAIM);
338 /* Has a disk mapping */
339 #define EROFS_MAP_MAPPED 0x0001
340 /* Located in metadata (could be copied from bd_inode) */
341 #define EROFS_MAP_META 0x0002
342 /* The extent is encoded */
343 #define EROFS_MAP_ENCODED 0x0004
344 /* The length of extent is full */
345 #define EROFS_MAP_FULL_MAPPED 0x0008
346 /* Located in the special packed inode */
347 #define EROFS_MAP_FRAGMENT 0x0010
348 /* The extent refers to partial decompressed data */
349 #define EROFS_MAP_PARTIAL_REF 0x0020
351 struct erofs_map_blocks {
352 struct erofs_buf buf;
354 erofs_off_t m_pa, m_la;
357 unsigned short m_deviceid;
358 char m_algorithmformat;
359 unsigned int m_flags;
363 * Used to get the exact decompressed length, e.g. fiemap (consider lookback
364 * approach instead if possible since it's more metadata lightweight.)
366 #define EROFS_GET_BLOCKS_FIEMAP 0x0001
367 /* Used to map the whole extent if non-negligible data is requested for LZMA */
368 #define EROFS_GET_BLOCKS_READMORE 0x0002
369 /* Used to map tail extent for tailpacking inline or fragment pcluster */
370 #define EROFS_GET_BLOCKS_FINDTAIL 0x0004
373 Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
374 Z_EROFS_COMPRESSION_INTERLACED,
375 Z_EROFS_COMPRESSION_RUNTIME_MAX
378 struct erofs_map_dev {
379 struct erofs_fscache *m_fscache;
380 struct block_device *m_bdev;
381 struct dax_device *m_daxdev;
385 unsigned int m_deviceid;
388 extern const struct super_operations erofs_sops;
390 extern const struct address_space_operations erofs_raw_access_aops;
391 extern const struct address_space_operations z_erofs_aops;
392 extern const struct address_space_operations erofs_fscache_access_aops;
394 extern const struct inode_operations erofs_generic_iops;
395 extern const struct inode_operations erofs_symlink_iops;
396 extern const struct inode_operations erofs_fast_symlink_iops;
397 extern const struct inode_operations erofs_dir_iops;
399 extern const struct file_operations erofs_file_fops;
400 extern const struct file_operations erofs_dir_fops;
402 extern const struct iomap_ops z_erofs_iomap_report_ops;
404 /* flags for erofs_fscache_register_cookie() */
405 #define EROFS_REG_COOKIE_SHARE 0x0001
406 #define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002
408 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
409 erofs_off_t *offset, int *lengthp);
410 void erofs_unmap_metabuf(struct erofs_buf *buf);
411 void erofs_put_metabuf(struct erofs_buf *buf);
412 void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
413 enum erofs_kmap_type type);
414 void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
415 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
416 erofs_blk_t blkaddr, enum erofs_kmap_type type);
417 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
418 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
420 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
421 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
422 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
423 struct kstat *stat, u32 request_mask,
424 unsigned int query_flags);
425 int erofs_namei(struct inode *dir, const struct qstr *name,
426 erofs_nid_t *nid, unsigned int *d_type);
428 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
433 void *p = vm_map_ram(pages, count, -1);
435 /* retry two more times (totally 3 times) */
436 if (p || ++retried >= 3)
443 int erofs_register_sysfs(struct super_block *sb);
444 void erofs_unregister_sysfs(struct super_block *sb);
445 int __init erofs_init_sysfs(void);
446 void erofs_exit_sysfs(void);
448 struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
449 static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
451 set_page_private(page, (unsigned long)*pagepool);
454 void erofs_release_pages(struct page **pagepool);
456 #ifdef CONFIG_EROFS_FS_ZIP
457 void erofs_workgroup_put(struct erofs_workgroup *grp);
458 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
460 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
461 struct erofs_workgroup *grp);
462 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
463 void erofs_shrinker_register(struct super_block *sb);
464 void erofs_shrinker_unregister(struct super_block *sb);
465 int __init erofs_init_shrinker(void);
466 void erofs_exit_shrinker(void);
467 int __init z_erofs_init_zip_subsystem(void);
468 void z_erofs_exit_zip_subsystem(void);
469 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
470 struct erofs_workgroup *egrp);
471 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
473 void *erofs_get_pcpubuf(unsigned int requiredpages);
474 void erofs_put_pcpubuf(void *ptr);
475 int erofs_pcpubuf_growsize(unsigned int nrpages);
476 void __init erofs_pcpubuf_init(void);
477 void erofs_pcpubuf_exit(void);
478 int erofs_init_managed_cache(struct super_block *sb);
479 int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb);
481 static inline void erofs_shrinker_register(struct super_block *sb) {}
482 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
483 static inline int erofs_init_shrinker(void) { return 0; }
484 static inline void erofs_exit_shrinker(void) {}
485 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
486 static inline void z_erofs_exit_zip_subsystem(void) {}
487 static inline void erofs_pcpubuf_init(void) {}
488 static inline void erofs_pcpubuf_exit(void) {}
489 static inline int erofs_init_managed_cache(struct super_block *sb) { return 0; }
490 #endif /* !CONFIG_EROFS_FS_ZIP */
492 #ifdef CONFIG_EROFS_FS_ZIP_LZMA
493 int __init z_erofs_lzma_init(void);
494 void z_erofs_lzma_exit(void);
496 static inline int z_erofs_lzma_init(void) { return 0; }
497 static inline int z_erofs_lzma_exit(void) { return 0; }
498 #endif /* !CONFIG_EROFS_FS_ZIP_LZMA */
500 #ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
501 int __init z_erofs_deflate_init(void);
502 void z_erofs_deflate_exit(void);
504 static inline int z_erofs_deflate_init(void) { return 0; }
505 static inline int z_erofs_deflate_exit(void) { return 0; }
506 #endif /* !CONFIG_EROFS_FS_ZIP_DEFLATE */
508 #ifdef CONFIG_EROFS_FS_ONDEMAND
509 int erofs_fscache_register_fs(struct super_block *sb);
510 void erofs_fscache_unregister_fs(struct super_block *sb);
512 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
513 char *name, unsigned int flags);
514 void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
516 static inline int erofs_fscache_register_fs(struct super_block *sb)
520 static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
523 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
524 char *name, unsigned int flags)
526 return ERR_PTR(-EOPNOTSUPP);
529 static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
534 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
536 #endif /* __EROFS_INTERNAL_H */