1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2021, Alibaba Cloud
6 */
7#ifndef __EROFS_INTERNAL_H
8#define __EROFS_INTERNAL_H
9
10#include <linux/fs.h>
11#include <linux/dax.h>
12#include <linux/dcache.h>
13#include <linux/mm.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>
21#include "erofs_fs.h"
22
23__printf(2, 3) void _erofs_printk(struct super_block *sb, const char *fmt, ...);
24#define erofs_err(sb, fmt, ...) \
25 _erofs_printk(sb, KERN_ERR fmt "\n", ##__VA_ARGS__)
26#define erofs_info(sb, fmt, ...) \
27 _erofs_printk(sb, KERN_INFO fmt "\n", ##__VA_ARGS__)
28
29#ifdef CONFIG_EROFS_FS_DEBUG
30#define DBG_BUGON BUG_ON
31#else
32#define DBG_BUGON(x) ((void)(x))
33#endif /* !CONFIG_EROFS_FS_DEBUG */
34
35/* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
36#define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
37
38typedef u64 erofs_nid_t;
39typedef u64 erofs_off_t;
40typedef u64 erofs_blk_t;
41
42struct erofs_device_info {
43 char *path;
44 struct erofs_fscache *fscache;
45 struct file *file;
46 struct dax_device *dax_dev;
47 u64 fsoff, dax_part_off;
48
49 erofs_blk_t blocks;
50 erofs_blk_t uniaddr;
51};
52
53enum {
54 EROFS_SYNC_DECOMPRESS_AUTO,
55 EROFS_SYNC_DECOMPRESS_FORCE_ON,
56 EROFS_SYNC_DECOMPRESS_FORCE_OFF
57};
58
59struct erofs_mount_opts {
60 /* current strategy of how to use managed cache */
61 unsigned char cache_strategy;
62 /* strategy of sync decompression (0 - auto, 1 - force on, 2 - force off) */
63 unsigned int sync_decompress;
64 /* threshold for decompression synchronously */
65 unsigned int max_sync_decompress_pages;
66 unsigned int mount_opt;
67};
68
69struct erofs_dev_context {
70 struct idr tree;
71 struct rw_semaphore rwsem;
72
73 unsigned int extra_devices;
74 bool flatdev;
75};
76
77/* all filesystem-wide lz4 configurations */
78struct erofs_sb_lz4_info {
79 /* # of pages needed for EROFS lz4 rolling decompression */
80 u16 max_distance_pages;
81 /* maximum possible blocks for pclusters in the filesystem */
82 u16 max_pclusterblks;
83};
84
85struct erofs_domain {
86 refcount_t ref;
87 struct list_head list;
88 struct fscache_volume *volume;
89 char *domain_id;
90};
91
92struct erofs_fscache {
93 struct fscache_cookie *cookie;
94 struct inode *inode; /* anonymous inode for the blob */
95
96 /* used for share domain mode */
97 struct erofs_domain *domain;
98 struct list_head node;
99 refcount_t ref;
100 char *name;
101};
102
103struct erofs_xattr_prefix_item {
104 struct erofs_xattr_long_prefix *prefix;
105 u8 infix_len;
106};
107
108struct erofs_sb_info {
109 struct erofs_device_info dif0;
110 struct erofs_mount_opts opt; /* options */
111#ifdef CONFIG_EROFS_FS_ZIP
112 /* list for all registered superblocks, mainly for shrinker */
113 struct list_head list;
114 struct mutex umount_mutex;
115
116 /* managed XArray arranged in physical block number */
117 struct xarray managed_pslots;
118
119 unsigned int shrinker_run_no;
120 u16 available_compr_algs;
121
122 /* pseudo inode to manage cached pages */
123 struct inode *managed_cache;
124
125 struct erofs_sb_lz4_info lz4;
126#endif /* CONFIG_EROFS_FS_ZIP */
127 struct inode *packed_inode;
128 struct inode *metabox_inode;
129 struct erofs_dev_context *devs;
130 u64 total_blocks;
131
132 u32 meta_blkaddr;
133#ifdef CONFIG_EROFS_FS_XATTR
134 u32 xattr_blkaddr;
135 u32 xattr_prefix_start;
136 u8 xattr_prefix_count;
137 struct erofs_xattr_prefix_item *xattr_prefixes;
138 unsigned int xattr_filter_reserved;
139#endif
140 u16 device_id_mask; /* valid bits of device id to be used */
141
142 unsigned char islotbits; /* inode slot unit size in bit shift */
143 unsigned char blkszbits; /* filesystem block size in bit shift */
144
145 u32 sb_size; /* total superblock size */
146 u32 fixed_nsec;
147 s64 epoch;
148
149 /* what we really care is nid, rather than ino.. */
150 erofs_nid_t root_nid;
151 erofs_nid_t packed_nid;
152 erofs_nid_t metabox_nid;
153 /* used for statfs, f_files - f_favail */
154 u64 inos;
155
156 char *volume_name;
157 u32 feature_compat;
158 u32 feature_incompat;
159
160 /* sysfs support */
161 struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
162 struct completion s_kobj_unregister;
163 erofs_off_t dir_ra_bytes;
164
165 /* fscache support */
166 struct fscache_volume *volume;
167 struct erofs_domain *domain;
168 char *fsid;
169 char *domain_id;
170};
171
172#define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
173#define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
174
175/* Mount flags set via mount options or defaults */
176#define EROFS_MOUNT_XATTR_USER 0x00000010
177#define EROFS_MOUNT_POSIX_ACL 0x00000020
178#define EROFS_MOUNT_DAX_ALWAYS 0x00000040
179#define EROFS_MOUNT_DAX_NEVER 0x00000080
180#define EROFS_MOUNT_DIRECT_IO 0x00000100
181
182#define clear_opt(opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)
183#define set_opt(opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)
184#define test_opt(opt, option) ((opt)->mount_opt & EROFS_MOUNT_##option)
185
186static inline bool erofs_is_fileio_mode(struct erofs_sb_info *sbi)
187{
188 return IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) && sbi->dif0.file;
189}
190
191static inline bool erofs_is_fscache_mode(struct super_block *sb)
192{
193 return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) &&
194 !erofs_is_fileio_mode(EROFS_SB(sb)) && !sb->s_bdev;
195}
196
197enum {
198 EROFS_ZIP_CACHE_DISABLED,
199 EROFS_ZIP_CACHE_READAHEAD,
200 EROFS_ZIP_CACHE_READAROUND
201};
202
203struct erofs_buf {
204 struct address_space *mapping;
205 struct file *file;
206 u64 off;
207 struct page *page;
208 void *base;
209};
210#define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL })
211
212#define erofs_blknr(sb, pos) ((erofs_blk_t)((pos) >> (sb)->s_blocksize_bits))
213#define erofs_blkoff(sb, pos) ((pos) & ((sb)->s_blocksize - 1))
214#define erofs_pos(sb, blk) ((erofs_off_t)(blk) << (sb)->s_blocksize_bits)
215#define erofs_iblks(i) (round_up((i)->i_size, i_blocksize(i)) >> (i)->i_blkbits)
216
217#define EROFS_FEATURE_FUNCS(name, compat, feature) \
218static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
219{ \
220 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
221}
222
223EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
224EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
225EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
226EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
227EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
228EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
229EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
230EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
231EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
232EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
233EROFS_FEATURE_FUNCS(48bit, incompat, INCOMPAT_48BIT)
234EROFS_FEATURE_FUNCS(metabox, incompat, INCOMPAT_METABOX)
235EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
236EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
237EROFS_FEATURE_FUNCS(shared_ea_in_metabox, compat, COMPAT_SHARED_EA_IN_METABOX)
238EROFS_FEATURE_FUNCS(plain_xattr_pfx, compat, COMPAT_PLAIN_XATTR_PFX)
239
240static inline u64 erofs_nid_to_ino64(struct erofs_sb_info *sbi, erofs_nid_t nid)
241{
242 if (!erofs_sb_has_metabox(sbi))
243 return nid;
244
245 /*
246 * When metadata compression is enabled, avoid generating excessively
247 * large inode numbers for metadata-compressed inodes. Shift NIDs in
248 * the 31-62 bit range left by one and move the metabox flag to bit 31.
249 *
250 * Note: on-disk NIDs remain unchanged as they are primarily used for
251 * compatibility with non-LFS 32-bit applications.
252 */
253 return ((nid << 1) & GENMASK_ULL(63, 32)) | (nid & GENMASK(30, 0)) |
254 ((nid >> EROFS_DIRENT_NID_METABOX_BIT) << 31);
255}
256
257/* atomic flag definitions */
258#define EROFS_I_EA_INITED_BIT 0
259#define EROFS_I_Z_INITED_BIT 1
260
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)
264
265/* default readahead size of directories */
266#define EROFS_DIR_RA_BYTES 16384
267
268struct erofs_inode {
269 erofs_nid_t nid;
270
271 /* atomic flags (including bitlocks) */
272 unsigned long flags;
273
274 unsigned char datalayout;
275 unsigned char inode_isize;
276 bool dot_omitted;
277 unsigned int xattr_isize;
278
279 unsigned int xattr_name_filter;
280 unsigned int xattr_shared_count;
281 unsigned int *xattr_shared_xattrs;
282
283 union {
284 erofs_blk_t startblk;
285 struct {
286 unsigned short chunkformat;
287 unsigned char chunkbits;
288 };
289#ifdef CONFIG_EROFS_FS_ZIP
290 struct {
291 unsigned short z_advise;
292 unsigned char z_algorithmtype[2];
293 unsigned char z_lclusterbits;
294 union {
295 u64 z_tailextent_headlcn;
296 u64 z_extents;
297 };
298 erofs_off_t z_fragmentoff;
299 unsigned short z_idata_size;
300 };
301#endif /* CONFIG_EROFS_FS_ZIP */
302 };
303 /* the corresponding vfs inode */
304 struct inode vfs_inode;
305};
306
307#define EROFS_I(ptr) container_of(ptr, struct erofs_inode, vfs_inode)
308
309static inline bool erofs_inode_in_metabox(struct inode *inode)
310{
311 return EROFS_I(inode)->nid & BIT_ULL(EROFS_DIRENT_NID_METABOX_BIT);
312}
313
314static inline erofs_off_t erofs_iloc(struct inode *inode)
315{
316 struct erofs_sb_info *sbi = EROFS_I_SB(inode);
317 erofs_nid_t nid_lo = EROFS_I(inode)->nid & EROFS_DIRENT_NID_MASK;
318
319 if (erofs_inode_in_metabox(inode))
320 return nid_lo << sbi->islotbits;
321 return erofs_pos(inode->i_sb, sbi->meta_blkaddr) +
322 (nid_lo << sbi->islotbits);
323}
324
325static inline unsigned int erofs_inode_version(unsigned int ifmt)
326{
327 return (ifmt >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASK;
328}
329
330static inline unsigned int erofs_inode_datalayout(unsigned int ifmt)
331{
332 return (ifmt >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASK;
333}
334
335/* reclaiming is never triggered when allocating new folios. */
336static inline struct folio *erofs_grab_folio_nowait(struct address_space *as,
337 pgoff_t index)
338{
339 return __filemap_get_folio(mapping: as, index,
340 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
341 gfp: readahead_gfp_mask(x: as) & ~__GFP_RECLAIM);
342}
343
344/* Has a disk mapping */
345#define EROFS_MAP_MAPPED 0x0001
346/* Located in metadata (could be copied from bd_inode) */
347#define EROFS_MAP_META 0x0002
348/* The extent is encoded */
349#define EROFS_MAP_ENCODED 0x0004
350/* The length of extent is full */
351#define EROFS_MAP_FULL_MAPPED 0x0008
352/* Located in the special packed inode */
353#define __EROFS_MAP_FRAGMENT 0x0010
354/* The extent refers to partial decompressed data */
355#define EROFS_MAP_PARTIAL_REF 0x0020
356
357#define EROFS_MAP_FRAGMENT (EROFS_MAP_MAPPED | __EROFS_MAP_FRAGMENT)
358
359struct erofs_map_blocks {
360 struct erofs_buf buf;
361
362 erofs_off_t m_pa, m_la;
363 u64 m_plen, m_llen;
364
365 unsigned short m_deviceid;
366 char m_algorithmformat;
367 unsigned int m_flags;
368};
369
370/*
371 * Used to get the exact decompressed length, e.g. fiemap (consider lookback
372 * approach instead if possible since it's more metadata lightweight.)
373 */
374#define EROFS_GET_BLOCKS_FIEMAP 0x0001
375/* Used to map the whole extent if non-negligible data is requested for LZMA */
376#define EROFS_GET_BLOCKS_READMORE 0x0002
377/* Used to map tail extent for tailpacking inline or fragment pcluster */
378#define EROFS_GET_BLOCKS_FINDTAIL 0x0004
379
380enum {
381 Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
382 Z_EROFS_COMPRESSION_INTERLACED,
383 Z_EROFS_COMPRESSION_RUNTIME_MAX
384};
385
386struct erofs_map_dev {
387 struct super_block *m_sb;
388 struct erofs_device_info *m_dif;
389 struct block_device *m_bdev;
390
391 erofs_off_t m_pa;
392 unsigned int m_deviceid;
393};
394
395extern const struct super_operations erofs_sops;
396
397extern const struct address_space_operations erofs_aops;
398extern const struct address_space_operations erofs_fileio_aops;
399extern const struct address_space_operations z_erofs_aops;
400extern const struct address_space_operations erofs_fscache_access_aops;
401
402extern const struct inode_operations erofs_generic_iops;
403extern const struct inode_operations erofs_symlink_iops;
404extern const struct inode_operations erofs_fast_symlink_iops;
405extern const struct inode_operations erofs_dir_iops;
406
407extern const struct file_operations erofs_file_fops;
408extern const struct file_operations erofs_dir_fops;
409
410extern const struct iomap_ops z_erofs_iomap_report_ops;
411
412/* flags for erofs_fscache_register_cookie() */
413#define EROFS_REG_COOKIE_SHARE 0x0001
414#define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002
415
416void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
417 erofs_off_t *offset, int *lengthp);
418void erofs_unmap_metabuf(struct erofs_buf *buf);
419void erofs_put_metabuf(struct erofs_buf *buf);
420void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap);
421int erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb,
422 bool in_metabox);
423void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
424 erofs_off_t offset, bool in_metabox);
425int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
426int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
427 u64 start, u64 len);
428int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
429void erofs_onlinefolio_init(struct folio *folio);
430void erofs_onlinefolio_split(struct folio *folio);
431void erofs_onlinefolio_end(struct folio *folio, int err, bool dirty);
432struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
433int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
434 struct kstat *stat, u32 request_mask,
435 unsigned int query_flags);
436int erofs_namei(struct inode *dir, const struct qstr *name,
437 erofs_nid_t *nid, unsigned int *d_type);
438
439static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
440{
441 int retried = 0;
442
443 while (1) {
444 void *p = vm_map_ram(pages, count, node: -1);
445
446 /* retry two more times (totally 3 times) */
447 if (p || ++retried >= 3)
448 return p;
449 vm_unmap_aliases();
450 }
451 return NULL;
452}
453
454int erofs_register_sysfs(struct super_block *sb);
455void erofs_unregister_sysfs(struct super_block *sb);
456int __init erofs_init_sysfs(void);
457void erofs_exit_sysfs(void);
458
459struct page *__erofs_allocpage(struct page **pagepool, gfp_t gfp, bool tryrsv);
460static inline struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp)
461{
462 return __erofs_allocpage(pagepool, gfp, tryrsv: false);
463}
464static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
465{
466 set_page_private(page, private: (unsigned long)*pagepool);
467 *pagepool = page;
468}
469void erofs_release_pages(struct page **pagepool);
470
471#ifdef CONFIG_EROFS_FS_ZIP
472#define MNGD_MAPPING(sbi) ((sbi)->managed_cache->i_mapping)
473
474extern atomic_long_t erofs_global_shrink_cnt;
475void erofs_shrinker_register(struct super_block *sb);
476void erofs_shrinker_unregister(struct super_block *sb);
477int __init erofs_init_shrinker(void);
478void erofs_exit_shrinker(void);
479int __init z_erofs_init_subsystem(void);
480void z_erofs_exit_subsystem(void);
481int z_erofs_init_super(struct super_block *sb);
482unsigned long z_erofs_shrink_scan(struct erofs_sb_info *sbi,
483 unsigned long nr_shrink);
484int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
485 int flags);
486void *z_erofs_get_gbuf(unsigned int requiredpages);
487void z_erofs_put_gbuf(void *ptr);
488int z_erofs_gbuf_growsize(unsigned int nrpages);
489int __init z_erofs_gbuf_init(void);
490void z_erofs_gbuf_exit(void);
491int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb);
492#else
493static inline void erofs_shrinker_register(struct super_block *sb) {}
494static inline void erofs_shrinker_unregister(struct super_block *sb) {}
495static inline int erofs_init_shrinker(void) { return 0; }
496static inline void erofs_exit_shrinker(void) {}
497static inline int z_erofs_init_subsystem(void) { return 0; }
498static inline void z_erofs_exit_subsystem(void) {}
499static inline int z_erofs_init_super(struct super_block *sb) { return 0; }
500#endif /* !CONFIG_EROFS_FS_ZIP */
501
502#ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
503struct bio *erofs_fileio_bio_alloc(struct erofs_map_dev *mdev);
504void erofs_fileio_submit_bio(struct bio *bio);
505#else
506static inline struct bio *erofs_fileio_bio_alloc(struct erofs_map_dev *mdev) { return NULL; }
507static inline void erofs_fileio_submit_bio(struct bio *bio) {}
508#endif
509
510#ifdef CONFIG_EROFS_FS_ONDEMAND
511int erofs_fscache_register_fs(struct super_block *sb);
512void erofs_fscache_unregister_fs(struct super_block *sb);
513
514struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
515 char *name, unsigned int flags);
516void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
517struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev);
518void erofs_fscache_submit_bio(struct bio *bio);
519#else
520static inline int erofs_fscache_register_fs(struct super_block *sb)
521{
522 return -EOPNOTSUPP;
523}
524static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
525
526static inline
527struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
528 char *name, unsigned int flags)
529{
530 return ERR_PTR(-EOPNOTSUPP);
531}
532
533static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
534{
535}
536static inline struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev) { return NULL; }
537static inline void erofs_fscache_submit_bio(struct bio *bio) {}
538#endif
539
540long erofs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
541long erofs_compat_ioctl(struct file *filp, unsigned int cmd,
542 unsigned long arg);
543
544#define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
545
546#endif /* __EROFS_INTERNAL_H */
547

source code of linux/fs/erofs/internal.h