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;
154 unsigned int xattr_filter_reserved;
156 u16 device_id_mask; /* valid bits of device id to be used */
158 unsigned char islotbits; /* inode slot unit size in bit shift */
159 unsigned char blkszbits; /* filesystem block size in bit shift */
161 u32 sb_size; /* total superblock size */
165 /* what we really care is nid, rather than ino.. */
166 erofs_nid_t root_nid;
167 erofs_nid_t packed_nid;
168 /* used for statfs, f_files - f_favail */
171 u8 uuid[16]; /* 128-bit uuid for volume */
172 u8 volume_name[16]; /* volume name */
174 u32 feature_incompat;
177 struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
178 struct completion s_kobj_unregister;
180 /* fscache support */
181 struct fscache_volume *volume;
182 struct erofs_fscache *s_fscache;
183 struct erofs_domain *domain;
188 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
189 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
191 /* Mount flags set via mount options or defaults */
192 #define EROFS_MOUNT_XATTR_USER 0x00000010
193 #define EROFS_MOUNT_POSIX_ACL 0x00000020
194 #define EROFS_MOUNT_DAX_ALWAYS 0x00000040
195 #define EROFS_MOUNT_DAX_NEVER 0x00000080
197 #define clear_opt(opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)
198 #define set_opt(opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)
199 #define test_opt(opt, option) ((opt)->mount_opt & EROFS_MOUNT_##option)
201 static inline bool erofs_is_fscache_mode(struct super_block *sb)
203 return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && !sb->s_bdev;
207 EROFS_ZIP_CACHE_DISABLED,
208 EROFS_ZIP_CACHE_READAHEAD,
209 EROFS_ZIP_CACHE_READAROUND
212 /* basic unit of the workstation of a super_block */
213 struct erofs_workgroup {
215 struct lockref lockref;
218 enum erofs_kmap_type {
219 EROFS_NO_KMAP, /* don't map the buffer */
220 EROFS_KMAP, /* use kmap_local_page() to map the buffer */
227 enum erofs_kmap_type kmap_type;
229 #define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL })
231 #define ROOT_NID(sb) ((sb)->root_nid)
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 struct file_system_type erofs_fs_type;
389 extern const struct super_operations erofs_sops;
391 extern const struct address_space_operations erofs_raw_access_aops;
392 extern const struct address_space_operations z_erofs_aops;
393 extern const struct address_space_operations erofs_fscache_access_aops;
395 extern const struct inode_operations erofs_generic_iops;
396 extern const struct inode_operations erofs_symlink_iops;
397 extern const struct inode_operations erofs_fast_symlink_iops;
398 extern const struct inode_operations erofs_dir_iops;
400 extern const struct file_operations erofs_file_fops;
401 extern const struct file_operations erofs_dir_fops;
403 extern const struct iomap_ops z_erofs_iomap_report_ops;
405 /* flags for erofs_fscache_register_cookie() */
406 #define EROFS_REG_COOKIE_SHARE 0x0001
407 #define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002
409 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
410 erofs_off_t *offset, int *lengthp);
411 void erofs_unmap_metabuf(struct erofs_buf *buf);
412 void erofs_put_metabuf(struct erofs_buf *buf);
413 void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
414 enum erofs_kmap_type type);
415 void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
416 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
417 erofs_blk_t blkaddr, enum erofs_kmap_type type);
418 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
419 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
421 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
422 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
423 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
424 struct kstat *stat, u32 request_mask,
425 unsigned int query_flags);
426 int erofs_namei(struct inode *dir, const struct qstr *name,
427 erofs_nid_t *nid, unsigned int *d_type);
429 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
434 void *p = vm_map_ram(pages, count, -1);
436 /* retry two more times (totally 3 times) */
437 if (p || ++retried >= 3)
444 int erofs_register_sysfs(struct super_block *sb);
445 void erofs_unregister_sysfs(struct super_block *sb);
446 int __init erofs_init_sysfs(void);
447 void erofs_exit_sysfs(void);
449 struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
450 static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
452 set_page_private(page, (unsigned long)*pagepool);
455 void erofs_release_pages(struct page **pagepool);
457 #ifdef CONFIG_EROFS_FS_ZIP
458 void erofs_workgroup_put(struct erofs_workgroup *grp);
459 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
461 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
462 struct erofs_workgroup *grp);
463 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
464 void erofs_shrinker_register(struct super_block *sb);
465 void erofs_shrinker_unregister(struct super_block *sb);
466 int __init erofs_init_shrinker(void);
467 void erofs_exit_shrinker(void);
468 int __init z_erofs_init_zip_subsystem(void);
469 void z_erofs_exit_zip_subsystem(void);
470 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
471 struct erofs_workgroup *egrp);
472 int z_erofs_load_lz4_config(struct super_block *sb,
473 struct erofs_super_block *dsb,
474 struct z_erofs_lz4_cfgs *lz4, int len);
475 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
477 void *erofs_get_pcpubuf(unsigned int requiredpages);
478 void erofs_put_pcpubuf(void *ptr);
479 int erofs_pcpubuf_growsize(unsigned int nrpages);
480 void __init erofs_pcpubuf_init(void);
481 void erofs_pcpubuf_exit(void);
482 int erofs_init_managed_cache(struct super_block *sb);
484 static inline void erofs_shrinker_register(struct super_block *sb) {}
485 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
486 static inline int erofs_init_shrinker(void) { return 0; }
487 static inline void erofs_exit_shrinker(void) {}
488 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
489 static inline void z_erofs_exit_zip_subsystem(void) {}
490 static inline int z_erofs_load_lz4_config(struct super_block *sb,
491 struct erofs_super_block *dsb,
492 struct z_erofs_lz4_cfgs *lz4, int len)
494 if (lz4 || dsb->u1.lz4_max_distance) {
495 erofs_err(sb, "lz4 algorithm isn't enabled");
500 static inline void erofs_pcpubuf_init(void) {}
501 static inline void erofs_pcpubuf_exit(void) {}
502 static inline int erofs_init_managed_cache(struct super_block *sb) { return 0; }
503 #endif /* !CONFIG_EROFS_FS_ZIP */
505 #ifdef CONFIG_EROFS_FS_ZIP_LZMA
506 int __init z_erofs_lzma_init(void);
507 void z_erofs_lzma_exit(void);
508 int z_erofs_load_lzma_config(struct super_block *sb,
509 struct erofs_super_block *dsb,
510 struct z_erofs_lzma_cfgs *lzma, int size);
512 static inline int z_erofs_lzma_init(void) { return 0; }
513 static inline int z_erofs_lzma_exit(void) { return 0; }
514 static inline int z_erofs_load_lzma_config(struct super_block *sb,
515 struct erofs_super_block *dsb,
516 struct z_erofs_lzma_cfgs *lzma, int size) {
518 erofs_err(sb, "lzma algorithm isn't enabled");
523 #endif /* !CONFIG_EROFS_FS_ZIP_LZMA */
525 #ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
526 int __init z_erofs_deflate_init(void);
527 void z_erofs_deflate_exit(void);
528 int z_erofs_load_deflate_config(struct super_block *sb,
529 struct erofs_super_block *dsb,
530 struct z_erofs_deflate_cfgs *dfl, int size);
532 static inline int z_erofs_deflate_init(void) { return 0; }
533 static inline int z_erofs_deflate_exit(void) { return 0; }
534 static inline int z_erofs_load_deflate_config(struct super_block *sb,
535 struct erofs_super_block *dsb,
536 struct z_erofs_deflate_cfgs *dfl, int size) {
538 erofs_err(sb, "deflate algorithm isn't enabled");
543 #endif /* !CONFIG_EROFS_FS_ZIP_DEFLATE */
545 #ifdef CONFIG_EROFS_FS_ONDEMAND
546 int erofs_fscache_register_fs(struct super_block *sb);
547 void erofs_fscache_unregister_fs(struct super_block *sb);
549 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
550 char *name, unsigned int flags);
551 void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
553 static inline int erofs_fscache_register_fs(struct super_block *sb)
557 static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
560 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
561 char *name, unsigned int flags)
563 return ERR_PTR(-EOPNOTSUPP);
566 static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
571 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
573 #endif /* __EROFS_INTERNAL_H */