Loading...
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/* redefine pr_fmt "erofs: " */
24#undef pr_fmt
25#define pr_fmt(fmt) "erofs: " fmt
26
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
37#else
38#define DBG_BUGON(x) ((void)(x))
39#endif /* !CONFIG_EROFS_FS_DEBUG */
40
41/* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
42#define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
43
44typedef u64 erofs_nid_t;
45typedef u64 erofs_off_t;
46/* data type for filesystem-wide blocks number */
47typedef u32 erofs_blk_t;
48
49struct erofs_device_info {
50 char *path;
51 struct erofs_fscache *fscache;
52 struct bdev_handle *bdev_handle;
53 struct dax_device *dax_dev;
54 u64 dax_part_off;
55
56 u32 blocks;
57 u32 mapped_blkaddr;
58};
59
60enum {
61 EROFS_SYNC_DECOMPRESS_AUTO,
62 EROFS_SYNC_DECOMPRESS_FORCE_ON,
63 EROFS_SYNC_DECOMPRESS_FORCE_OFF
64};
65
66struct 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;
72
73 /* threshold for decompression synchronously */
74 unsigned int max_sync_decompress_pages;
75#endif
76 unsigned int mount_opt;
77};
78
79struct erofs_dev_context {
80 struct idr tree;
81 struct rw_semaphore rwsem;
82
83 unsigned int extra_devices;
84 bool flatdev;
85};
86
87struct erofs_fs_context {
88 struct erofs_mount_opts opt;
89 struct erofs_dev_context *devs;
90 char *fsid;
91 char *domain_id;
92};
93
94/* all filesystem-wide lz4 configurations */
95struct 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 */
99 u16 max_pclusterblks;
100};
101
102struct erofs_domain {
103 refcount_t ref;
104 struct list_head list;
105 struct fscache_volume *volume;
106 char *domain_id;
107};
108
109struct erofs_fscache {
110 struct fscache_cookie *cookie;
111 struct inode *inode; /* anonymous inode for the blob */
112
113 /* used for share domain mode */
114 struct erofs_domain *domain;
115 struct list_head node;
116 refcount_t ref;
117 char *name;
118};
119
120struct erofs_xattr_prefix_item {
121 struct erofs_xattr_long_prefix *prefix;
122 u8 infix_len;
123};
124
125struct 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;
131
132 /* managed XArray arranged in physical block number */
133 struct xarray managed_pslots;
134
135 unsigned int shrinker_run_no;
136 u16 available_compr_algs;
137
138 /* pseudo inode to manage cached pages */
139 struct inode *managed_cache;
140
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;
146 u64 dax_part_off;
147 u64 total_blocks;
148 u32 primarydevice_blocks;
149
150 u32 meta_blkaddr;
151#ifdef CONFIG_EROFS_FS_XATTR
152 u32 xattr_blkaddr;
153 u32 xattr_prefix_start;
154 u8 xattr_prefix_count;
155 struct erofs_xattr_prefix_item *xattr_prefixes;
156 unsigned int xattr_filter_reserved;
157#endif
158 u16 device_id_mask; /* valid bits of device id to be used */
159
160 unsigned char islotbits; /* inode slot unit size in bit shift */
161 unsigned char blkszbits; /* filesystem block size in bit shift */
162
163 u32 sb_size; /* total superblock size */
164 u32 build_time_nsec;
165 u64 build_time;
166
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 */
171 u64 inos;
172
173 u8 uuid[16]; /* 128-bit uuid for volume */
174 u8 volume_name[16]; /* volume name */
175 u32 feature_compat;
176 u32 feature_incompat;
177
178 /* sysfs support */
179 struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
180 struct completion s_kobj_unregister;
181
182 /* fscache support */
183 struct fscache_volume *volume;
184 struct erofs_fscache *s_fscache;
185 struct erofs_domain *domain;
186 char *fsid;
187 char *domain_id;
188};
189
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)
192
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
198
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)
202
203static inline bool erofs_is_fscache_mode(struct super_block *sb)
204{
205 return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && !sb->s_bdev;
206}
207
208enum {
209 EROFS_ZIP_CACHE_DISABLED,
210 EROFS_ZIP_CACHE_READAHEAD,
211 EROFS_ZIP_CACHE_READAROUND
212};
213
214/* basic unit of the workstation of a super_block */
215struct erofs_workgroup {
216 pgoff_t index;
217 struct lockref lockref;
218};
219
220enum erofs_kmap_type {
221 EROFS_NO_KMAP, /* don't map the buffer */
222 EROFS_KMAP, /* use kmap_local_page() to map the buffer */
223};
224
225struct erofs_buf {
226 struct inode *inode;
227 struct page *page;
228 void *base;
229 enum erofs_kmap_type kmap_type;
230};
231#define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL })
232
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)
237
238#define EROFS_FEATURE_FUNCS(name, compat, feature) \
239static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
240{ \
241 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
242}
243
244EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
245EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
246EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
247EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
248EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
249EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
250EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
251EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
252EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
253EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
254EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
255EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
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
265struct erofs_inode {
266 erofs_nid_t nid;
267
268 /* atomic flags (including bitlocks) */
269 unsigned long flags;
270
271 unsigned char datalayout;
272 unsigned char inode_isize;
273 unsigned int xattr_isize;
274
275 unsigned int xattr_name_filter;
276 unsigned int xattr_shared_count;
277 unsigned int *xattr_shared_xattrs;
278
279 union {
280 erofs_blk_t raw_blkaddr;
281 struct {
282 unsigned short chunkformat;
283 unsigned char chunkbits;
284 };
285#ifdef CONFIG_EROFS_FS_ZIP
286 struct {
287 unsigned short z_advise;
288 unsigned char z_algorithmtype[2];
289 unsigned char z_logical_clusterbits;
290 unsigned long z_tailextent_headlcn;
291 union {
292 struct {
293 erofs_off_t z_idataoff;
294 unsigned short z_idata_size;
295 };
296 erofs_off_t z_fragmentoff;
297 };
298 };
299#endif /* CONFIG_EROFS_FS_ZIP */
300 };
301 /* the corresponding vfs inode */
302 struct inode vfs_inode;
303};
304
305#define EROFS_I(ptr) container_of(ptr, struct erofs_inode, vfs_inode)
306
307static inline erofs_off_t erofs_iloc(struct inode *inode)
308{
309 struct erofs_sb_info *sbi = EROFS_I_SB(inode);
310
311 return erofs_pos(inode->i_sb, sbi->meta_blkaddr) +
312 (EROFS_I(inode)->nid << sbi->islotbits);
313}
314
315static inline unsigned int erofs_inode_version(unsigned int ifmt)
316{
317 return (ifmt >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASK;
318}
319
320static inline unsigned int erofs_inode_datalayout(unsigned int ifmt)
321{
322 return (ifmt >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASK;
323}
324
325/*
326 * Different from grab_cache_page_nowait(), reclaiming is never triggered
327 * when allocating new pages.
328 */
329static inline
330struct page *erofs_grab_cache_page_nowait(struct address_space *mapping,
331 pgoff_t index)
332{
333 return pagecache_get_page(mapping, index,
334 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
335 readahead_gfp_mask(mapping) & ~__GFP_RECLAIM);
336}
337
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
350
351struct erofs_map_blocks {
352 struct erofs_buf buf;
353
354 erofs_off_t m_pa, m_la;
355 u64 m_plen, m_llen;
356
357 unsigned short m_deviceid;
358 char m_algorithmformat;
359 unsigned int m_flags;
360};
361
362/*
363 * Used to get the exact decompressed length, e.g. fiemap (consider lookback
364 * approach instead if possible since it's more metadata lightweight.)
365 */
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
371
372enum {
373 Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
374 Z_EROFS_COMPRESSION_INTERLACED,
375 Z_EROFS_COMPRESSION_RUNTIME_MAX
376};
377
378struct erofs_map_dev {
379 struct erofs_fscache *m_fscache;
380 struct block_device *m_bdev;
381 struct dax_device *m_daxdev;
382 u64 m_dax_part_off;
383
384 erofs_off_t m_pa;
385 unsigned int m_deviceid;
386};
387
388extern struct file_system_type erofs_fs_type;
389extern const struct super_operations erofs_sops;
390
391extern const struct address_space_operations erofs_raw_access_aops;
392extern const struct address_space_operations z_erofs_aops;
393extern const struct address_space_operations erofs_fscache_access_aops;
394
395extern const struct inode_operations erofs_generic_iops;
396extern const struct inode_operations erofs_symlink_iops;
397extern const struct inode_operations erofs_fast_symlink_iops;
398extern const struct inode_operations erofs_dir_iops;
399
400extern const struct file_operations erofs_file_fops;
401extern const struct file_operations erofs_dir_fops;
402
403extern const struct iomap_ops z_erofs_iomap_report_ops;
404
405/* flags for erofs_fscache_register_cookie() */
406#define EROFS_REG_COOKIE_SHARE 0x0001
407#define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002
408
409void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
410 erofs_off_t *offset, int *lengthp);
411void erofs_unmap_metabuf(struct erofs_buf *buf);
412void erofs_put_metabuf(struct erofs_buf *buf);
413void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
414 enum erofs_kmap_type type);
415void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
416void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
417 erofs_blk_t blkaddr, enum erofs_kmap_type type);
418int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
419int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
420 u64 start, u64 len);
421int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
422struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
423int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
424 struct kstat *stat, u32 request_mask,
425 unsigned int query_flags);
426int erofs_namei(struct inode *dir, const struct qstr *name,
427 erofs_nid_t *nid, unsigned int *d_type);
428
429static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
430{
431 int retried = 0;
432
433 while (1) {
434 void *p = vm_map_ram(pages, count, -1);
435
436 /* retry two more times (totally 3 times) */
437 if (p || ++retried >= 3)
438 return p;
439 vm_unmap_aliases();
440 }
441 return NULL;
442}
443
444int erofs_register_sysfs(struct super_block *sb);
445void erofs_unregister_sysfs(struct super_block *sb);
446int __init erofs_init_sysfs(void);
447void erofs_exit_sysfs(void);
448
449struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
450static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
451{
452 set_page_private(page, (unsigned long)*pagepool);
453 *pagepool = page;
454}
455void erofs_release_pages(struct page **pagepool);
456
457#ifdef CONFIG_EROFS_FS_ZIP
458void erofs_workgroup_put(struct erofs_workgroup *grp);
459struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
460 pgoff_t index);
461struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
462 struct erofs_workgroup *grp);
463void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
464void erofs_shrinker_register(struct super_block *sb);
465void erofs_shrinker_unregister(struct super_block *sb);
466int __init erofs_init_shrinker(void);
467void erofs_exit_shrinker(void);
468int __init z_erofs_init_zip_subsystem(void);
469void z_erofs_exit_zip_subsystem(void);
470int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
471 struct erofs_workgroup *egrp);
472int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
473 int flags);
474void *erofs_get_pcpubuf(unsigned int requiredpages);
475void erofs_put_pcpubuf(void *ptr);
476int erofs_pcpubuf_growsize(unsigned int nrpages);
477void __init erofs_pcpubuf_init(void);
478void erofs_pcpubuf_exit(void);
479int erofs_init_managed_cache(struct super_block *sb);
480int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb);
481#else
482static inline void erofs_shrinker_register(struct super_block *sb) {}
483static inline void erofs_shrinker_unregister(struct super_block *sb) {}
484static inline int erofs_init_shrinker(void) { return 0; }
485static inline void erofs_exit_shrinker(void) {}
486static inline int z_erofs_init_zip_subsystem(void) { return 0; }
487static inline void z_erofs_exit_zip_subsystem(void) {}
488static inline void erofs_pcpubuf_init(void) {}
489static inline void erofs_pcpubuf_exit(void) {}
490static inline int erofs_init_managed_cache(struct super_block *sb) { return 0; }
491#endif /* !CONFIG_EROFS_FS_ZIP */
492
493#ifdef CONFIG_EROFS_FS_ZIP_LZMA
494int __init z_erofs_lzma_init(void);
495void z_erofs_lzma_exit(void);
496#else
497static inline int z_erofs_lzma_init(void) { return 0; }
498static inline int z_erofs_lzma_exit(void) { return 0; }
499#endif /* !CONFIG_EROFS_FS_ZIP_LZMA */
500
501#ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
502int __init z_erofs_deflate_init(void);
503void z_erofs_deflate_exit(void);
504#else
505static inline int z_erofs_deflate_init(void) { return 0; }
506static inline int z_erofs_deflate_exit(void) { return 0; }
507#endif /* !CONFIG_EROFS_FS_ZIP_DEFLATE */
508
509#ifdef CONFIG_EROFS_FS_ONDEMAND
510int erofs_fscache_register_fs(struct super_block *sb);
511void erofs_fscache_unregister_fs(struct super_block *sb);
512
513struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
514 char *name, unsigned int flags);
515void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
516#else
517static inline int erofs_fscache_register_fs(struct super_block *sb)
518{
519 return -EOPNOTSUPP;
520}
521static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
522
523static inline
524struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
525 char *name, unsigned int flags)
526{
527 return ERR_PTR(-EOPNOTSUPP);
528}
529
530static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
531{
532}
533#endif
534
535#define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
536
537#endif /* __EROFS_INTERNAL_H */
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/* redefine pr_fmt "erofs: " */
24#undef pr_fmt
25#define pr_fmt(fmt) "erofs: " fmt
26
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
37#else
38#define DBG_BUGON(x) ((void)(x))
39#endif /* !CONFIG_EROFS_FS_DEBUG */
40
41/* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
42#define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
43
44typedef u64 erofs_nid_t;
45typedef u64 erofs_off_t;
46/* data type for filesystem-wide blocks number */
47typedef u32 erofs_blk_t;
48
49struct erofs_device_info {
50 char *path;
51 struct erofs_fscache *fscache;
52 struct file *bdev_file;
53 struct dax_device *dax_dev;
54 u64 dax_part_off;
55
56 u32 blocks;
57 u32 mapped_blkaddr;
58};
59
60enum {
61 EROFS_SYNC_DECOMPRESS_AUTO,
62 EROFS_SYNC_DECOMPRESS_FORCE_ON,
63 EROFS_SYNC_DECOMPRESS_FORCE_OFF
64};
65
66struct 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;
72
73 /* threshold for decompression synchronously */
74 unsigned int max_sync_decompress_pages;
75#endif
76 unsigned int mount_opt;
77};
78
79struct erofs_dev_context {
80 struct idr tree;
81 struct rw_semaphore rwsem;
82
83 unsigned int extra_devices;
84 bool flatdev;
85};
86
87/* all filesystem-wide lz4 configurations */
88struct 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 */
92 u16 max_pclusterblks;
93};
94
95struct erofs_domain {
96 refcount_t ref;
97 struct list_head list;
98 struct fscache_volume *volume;
99 char *domain_id;
100};
101
102struct erofs_fscache {
103 struct fscache_cookie *cookie;
104 struct inode *inode; /* anonymous inode for the blob */
105
106 /* used for share domain mode */
107 struct erofs_domain *domain;
108 struct list_head node;
109 refcount_t ref;
110 char *name;
111};
112
113struct erofs_xattr_prefix_item {
114 struct erofs_xattr_long_prefix *prefix;
115 u8 infix_len;
116};
117
118struct 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;
124
125 /* managed XArray arranged in physical block number */
126 struct xarray managed_pslots;
127
128 unsigned int shrinker_run_no;
129 u16 available_compr_algs;
130
131 /* pseudo inode to manage cached pages */
132 struct inode *managed_cache;
133
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;
139 u64 dax_part_off;
140 u64 total_blocks;
141 u32 primarydevice_blocks;
142
143 u32 meta_blkaddr;
144#ifdef CONFIG_EROFS_FS_XATTR
145 u32 xattr_blkaddr;
146 u32 xattr_prefix_start;
147 u8 xattr_prefix_count;
148 struct erofs_xattr_prefix_item *xattr_prefixes;
149 unsigned int xattr_filter_reserved;
150#endif
151 u16 device_id_mask; /* valid bits of device id to be used */
152
153 unsigned char islotbits; /* inode slot unit size in bit shift */
154 unsigned char blkszbits; /* filesystem block size in bit shift */
155
156 u32 sb_size; /* total superblock size */
157 u32 build_time_nsec;
158 u64 build_time;
159
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 */
164 u64 inos;
165
166 u8 uuid[16]; /* 128-bit uuid for volume */
167 u8 volume_name[16]; /* volume name */
168 u32 feature_compat;
169 u32 feature_incompat;
170
171 /* sysfs support */
172 struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
173 struct completion s_kobj_unregister;
174
175 /* fscache support */
176 struct fscache_volume *volume;
177 struct erofs_fscache *s_fscache;
178 struct erofs_domain *domain;
179 char *fsid;
180 char *domain_id;
181};
182
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)
185
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
191
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)
195
196static inline bool erofs_is_fscache_mode(struct super_block *sb)
197{
198 return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && !sb->s_bdev;
199}
200
201enum {
202 EROFS_ZIP_CACHE_DISABLED,
203 EROFS_ZIP_CACHE_READAHEAD,
204 EROFS_ZIP_CACHE_READAROUND
205};
206
207/* basic unit of the workstation of a super_block */
208struct erofs_workgroup {
209 pgoff_t index;
210 struct lockref lockref;
211};
212
213enum erofs_kmap_type {
214 EROFS_NO_KMAP, /* don't map the buffer */
215 EROFS_KMAP, /* use kmap_local_page() to map the buffer */
216};
217
218struct erofs_buf {
219 struct inode *inode;
220 struct page *page;
221 void *base;
222 enum erofs_kmap_type kmap_type;
223};
224#define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL })
225
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)
230
231#define EROFS_FEATURE_FUNCS(name, compat, feature) \
232static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
233{ \
234 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
235}
236
237EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
238EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
239EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
240EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
241EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
242EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
243EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
244EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
245EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
246EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
247EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
248EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
249
250/* atomic flag definitions */
251#define EROFS_I_EA_INITED_BIT 0
252#define EROFS_I_Z_INITED_BIT 1
253
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)
257
258struct erofs_inode {
259 erofs_nid_t nid;
260
261 /* atomic flags (including bitlocks) */
262 unsigned long flags;
263
264 unsigned char datalayout;
265 unsigned char inode_isize;
266 unsigned int xattr_isize;
267
268 unsigned int xattr_name_filter;
269 unsigned int xattr_shared_count;
270 unsigned int *xattr_shared_xattrs;
271
272 union {
273 erofs_blk_t raw_blkaddr;
274 struct {
275 unsigned short chunkformat;
276 unsigned char chunkbits;
277 };
278#ifdef CONFIG_EROFS_FS_ZIP
279 struct {
280 unsigned short z_advise;
281 unsigned char z_algorithmtype[2];
282 unsigned char z_logical_clusterbits;
283 unsigned long z_tailextent_headlcn;
284 union {
285 struct {
286 erofs_off_t z_idataoff;
287 unsigned short z_idata_size;
288 };
289 erofs_off_t z_fragmentoff;
290 };
291 };
292#endif /* CONFIG_EROFS_FS_ZIP */
293 };
294 /* the corresponding vfs inode */
295 struct inode vfs_inode;
296};
297
298#define EROFS_I(ptr) container_of(ptr, struct erofs_inode, vfs_inode)
299
300static inline erofs_off_t erofs_iloc(struct inode *inode)
301{
302 struct erofs_sb_info *sbi = EROFS_I_SB(inode);
303
304 return erofs_pos(inode->i_sb, sbi->meta_blkaddr) +
305 (EROFS_I(inode)->nid << sbi->islotbits);
306}
307
308static inline unsigned int erofs_inode_version(unsigned int ifmt)
309{
310 return (ifmt >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASK;
311}
312
313static inline unsigned int erofs_inode_datalayout(unsigned int ifmt)
314{
315 return (ifmt >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASK;
316}
317
318/*
319 * Different from grab_cache_page_nowait(), reclaiming is never triggered
320 * when allocating new pages.
321 */
322static inline
323struct page *erofs_grab_cache_page_nowait(struct address_space *mapping,
324 pgoff_t index)
325{
326 return pagecache_get_page(mapping, index,
327 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
328 readahead_gfp_mask(mapping) & ~__GFP_RECLAIM);
329}
330
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
343
344struct erofs_map_blocks {
345 struct erofs_buf buf;
346
347 erofs_off_t m_pa, m_la;
348 u64 m_plen, m_llen;
349
350 unsigned short m_deviceid;
351 char m_algorithmformat;
352 unsigned int m_flags;
353};
354
355/*
356 * Used to get the exact decompressed length, e.g. fiemap (consider lookback
357 * approach instead if possible since it's more metadata lightweight.)
358 */
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
364
365enum {
366 Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
367 Z_EROFS_COMPRESSION_INTERLACED,
368 Z_EROFS_COMPRESSION_RUNTIME_MAX
369};
370
371struct erofs_map_dev {
372 struct erofs_fscache *m_fscache;
373 struct block_device *m_bdev;
374 struct dax_device *m_daxdev;
375 u64 m_dax_part_off;
376
377 erofs_off_t m_pa;
378 unsigned int m_deviceid;
379};
380
381extern const struct super_operations erofs_sops;
382
383extern const struct address_space_operations erofs_raw_access_aops;
384extern const struct address_space_operations z_erofs_aops;
385extern const struct address_space_operations erofs_fscache_access_aops;
386
387extern const struct inode_operations erofs_generic_iops;
388extern const struct inode_operations erofs_symlink_iops;
389extern const struct inode_operations erofs_fast_symlink_iops;
390extern const struct inode_operations erofs_dir_iops;
391
392extern const struct file_operations erofs_file_fops;
393extern const struct file_operations erofs_dir_fops;
394
395extern const struct iomap_ops z_erofs_iomap_report_ops;
396
397/* flags for erofs_fscache_register_cookie() */
398#define EROFS_REG_COOKIE_SHARE 0x0001
399#define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002
400
401void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
402 erofs_off_t *offset, int *lengthp);
403void erofs_unmap_metabuf(struct erofs_buf *buf);
404void erofs_put_metabuf(struct erofs_buf *buf);
405void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
406 enum erofs_kmap_type type);
407void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
408void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
409 erofs_blk_t blkaddr, enum erofs_kmap_type type);
410int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
411int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
412 u64 start, u64 len);
413int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
414struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
415int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
416 struct kstat *stat, u32 request_mask,
417 unsigned int query_flags);
418int erofs_namei(struct inode *dir, const struct qstr *name,
419 erofs_nid_t *nid, unsigned int *d_type);
420
421static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
422{
423 int retried = 0;
424
425 while (1) {
426 void *p = vm_map_ram(pages, count, -1);
427
428 /* retry two more times (totally 3 times) */
429 if (p || ++retried >= 3)
430 return p;
431 vm_unmap_aliases();
432 }
433 return NULL;
434}
435
436int erofs_register_sysfs(struct super_block *sb);
437void erofs_unregister_sysfs(struct super_block *sb);
438int __init erofs_init_sysfs(void);
439void erofs_exit_sysfs(void);
440
441struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
442static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
443{
444 set_page_private(page, (unsigned long)*pagepool);
445 *pagepool = page;
446}
447void erofs_release_pages(struct page **pagepool);
448
449#ifdef CONFIG_EROFS_FS_ZIP
450void erofs_workgroup_put(struct erofs_workgroup *grp);
451struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
452 pgoff_t index);
453struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
454 struct erofs_workgroup *grp);
455void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
456void erofs_shrinker_register(struct super_block *sb);
457void erofs_shrinker_unregister(struct super_block *sb);
458int __init erofs_init_shrinker(void);
459void erofs_exit_shrinker(void);
460int __init z_erofs_init_zip_subsystem(void);
461void z_erofs_exit_zip_subsystem(void);
462int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,
463 struct erofs_workgroup *egrp);
464int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
465 int flags);
466void *erofs_get_pcpubuf(unsigned int requiredpages);
467void erofs_put_pcpubuf(void *ptr);
468int erofs_pcpubuf_growsize(unsigned int nrpages);
469void __init erofs_pcpubuf_init(void);
470void erofs_pcpubuf_exit(void);
471int erofs_init_managed_cache(struct super_block *sb);
472int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb);
473#else
474static inline void erofs_shrinker_register(struct super_block *sb) {}
475static inline void erofs_shrinker_unregister(struct super_block *sb) {}
476static inline int erofs_init_shrinker(void) { return 0; }
477static inline void erofs_exit_shrinker(void) {}
478static inline int z_erofs_init_zip_subsystem(void) { return 0; }
479static inline void z_erofs_exit_zip_subsystem(void) {}
480static inline void erofs_pcpubuf_init(void) {}
481static inline void erofs_pcpubuf_exit(void) {}
482static inline int erofs_init_managed_cache(struct super_block *sb) { return 0; }
483#endif /* !CONFIG_EROFS_FS_ZIP */
484
485#ifdef CONFIG_EROFS_FS_ZIP_LZMA
486int __init z_erofs_lzma_init(void);
487void z_erofs_lzma_exit(void);
488#else
489static inline int z_erofs_lzma_init(void) { return 0; }
490static inline int z_erofs_lzma_exit(void) { return 0; }
491#endif /* !CONFIG_EROFS_FS_ZIP_LZMA */
492
493#ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
494int __init z_erofs_deflate_init(void);
495void z_erofs_deflate_exit(void);
496#else
497static inline int z_erofs_deflate_init(void) { return 0; }
498static inline int z_erofs_deflate_exit(void) { return 0; }
499#endif /* !CONFIG_EROFS_FS_ZIP_DEFLATE */
500
501#ifdef CONFIG_EROFS_FS_ONDEMAND
502int erofs_fscache_register_fs(struct super_block *sb);
503void erofs_fscache_unregister_fs(struct super_block *sb);
504
505struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
506 char *name, unsigned int flags);
507void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
508struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev);
509void erofs_fscache_submit_bio(struct bio *bio);
510#else
511static inline int erofs_fscache_register_fs(struct super_block *sb)
512{
513 return -EOPNOTSUPP;
514}
515static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
516
517static inline
518struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
519 char *name, unsigned int flags)
520{
521 return ERR_PTR(-EOPNOTSUPP);
522}
523
524static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
525{
526}
527static inline struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev) { return NULL; }
528static inline void erofs_fscache_submit_bio(struct bio *bio) {}
529#endif
530
531#define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
532
533#endif /* __EROFS_INTERNAL_H */