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 */
6#ifndef __EROFS_INTERNAL_H
7#define __EROFS_INTERNAL_H
8
9#include <linux/fs.h>
10#include <linux/dcache.h>
11#include <linux/mm.h>
12#include <linux/pagemap.h>
13#include <linux/bio.h>
14#include <linux/buffer_head.h>
15#include <linux/magic.h>
16#include <linux/slab.h>
17#include <linux/vmalloc.h>
18#include "erofs_fs.h"
19
20/* redefine pr_fmt "erofs: " */
21#undef pr_fmt
22#define pr_fmt(fmt) "erofs: " fmt
23
24__printf(3, 4) void _erofs_err(struct super_block *sb,
25 const char *function, const char *fmt, ...);
26#define erofs_err(sb, fmt, ...) \
27 _erofs_err(sb, __func__, fmt "\n", ##__VA_ARGS__)
28__printf(3, 4) void _erofs_info(struct super_block *sb,
29 const char *function, const char *fmt, ...);
30#define erofs_info(sb, fmt, ...) \
31 _erofs_info(sb, __func__, fmt "\n", ##__VA_ARGS__)
32#ifdef CONFIG_EROFS_FS_DEBUG
33#define erofs_dbg(x, ...) pr_debug(x "\n", ##__VA_ARGS__)
34#define DBG_BUGON BUG_ON
35#else
36#define erofs_dbg(x, ...) ((void)0)
37#define DBG_BUGON(x) ((void)(x))
38#endif /* !CONFIG_EROFS_FS_DEBUG */
39
40/* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
41#define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
42
43typedef u64 erofs_nid_t;
44typedef u64 erofs_off_t;
45/* data type for filesystem-wide blocks number */
46typedef u32 erofs_blk_t;
47
48struct erofs_fs_context {
49#ifdef CONFIG_EROFS_FS_ZIP
50 /* current strategy of how to use managed cache */
51 unsigned char cache_strategy;
52 /* strategy of sync decompression (false - auto, true - force on) */
53 bool readahead_sync_decompress;
54
55 /* threshold for decompression synchronously */
56 unsigned int max_sync_decompress_pages;
57#endif
58 unsigned int mount_opt;
59};
60
61/* all filesystem-wide lz4 configurations */
62struct erofs_sb_lz4_info {
63 /* # of pages needed for EROFS lz4 rolling decompression */
64 u16 max_distance_pages;
65 /* maximum possible blocks for pclusters in the filesystem */
66 u16 max_pclusterblks;
67};
68
69struct erofs_sb_info {
70#ifdef CONFIG_EROFS_FS_ZIP
71 /* list for all registered superblocks, mainly for shrinker */
72 struct list_head list;
73 struct mutex umount_mutex;
74
75 /* managed XArray arranged in physical block number */
76 struct xarray managed_pslots;
77
78 unsigned int shrinker_run_no;
79 u16 available_compr_algs;
80
81 /* pseudo inode to manage cached pages */
82 struct inode *managed_cache;
83
84 struct erofs_sb_lz4_info lz4;
85#endif /* CONFIG_EROFS_FS_ZIP */
86 u32 blocks;
87 u32 meta_blkaddr;
88#ifdef CONFIG_EROFS_FS_XATTR
89 u32 xattr_blkaddr;
90#endif
91
92 /* inode slot unit size in bit shift */
93 unsigned char islotbits;
94
95 u32 sb_size; /* total superblock size */
96 u32 build_time_nsec;
97 u64 build_time;
98
99 /* what we really care is nid, rather than ino.. */
100 erofs_nid_t root_nid;
101 /* used for statfs, f_files - f_favail */
102 u64 inos;
103
104 u8 uuid[16]; /* 128-bit uuid for volume */
105 u8 volume_name[16]; /* volume name */
106 u32 feature_compat;
107 u32 feature_incompat;
108
109 struct erofs_fs_context ctx; /* options */
110};
111
112#define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
113#define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
114
115/* Mount flags set via mount options or defaults */
116#define EROFS_MOUNT_XATTR_USER 0x00000010
117#define EROFS_MOUNT_POSIX_ACL 0x00000020
118
119#define clear_opt(ctx, option) ((ctx)->mount_opt &= ~EROFS_MOUNT_##option)
120#define set_opt(ctx, option) ((ctx)->mount_opt |= EROFS_MOUNT_##option)
121#define test_opt(ctx, option) ((ctx)->mount_opt & EROFS_MOUNT_##option)
122
123enum {
124 EROFS_ZIP_CACHE_DISABLED,
125 EROFS_ZIP_CACHE_READAHEAD,
126 EROFS_ZIP_CACHE_READAROUND
127};
128
129#ifdef CONFIG_EROFS_FS_ZIP
130#define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL)
131
132/* basic unit of the workstation of a super_block */
133struct erofs_workgroup {
134 /* the workgroup index in the workstation */
135 pgoff_t index;
136
137 /* overall workgroup reference count */
138 atomic_t refcount;
139};
140
141#if defined(CONFIG_SMP)
142static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
143 int val)
144{
145 preempt_disable();
146 if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
147 preempt_enable();
148 return false;
149 }
150 return true;
151}
152
153static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
154 int orig_val)
155{
156 /*
157 * other observers should notice all modifications
158 * in the freezing period.
159 */
160 smp_mb();
161 atomic_set(&grp->refcount, orig_val);
162 preempt_enable();
163}
164
165static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
166{
167 return atomic_cond_read_relaxed(&grp->refcount,
168 VAL != EROFS_LOCKED_MAGIC);
169}
170#else
171static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
172 int val)
173{
174 preempt_disable();
175 /* no need to spin on UP platforms, let's just disable preemption. */
176 if (val != atomic_read(&grp->refcount)) {
177 preempt_enable();
178 return false;
179 }
180 return true;
181}
182
183static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
184 int orig_val)
185{
186 preempt_enable();
187}
188
189static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
190{
191 int v = atomic_read(&grp->refcount);
192
193 /* workgroup is never freezed on uniprocessor systems */
194 DBG_BUGON(v == EROFS_LOCKED_MAGIC);
195 return v;
196}
197#endif /* !CONFIG_SMP */
198#endif /* !CONFIG_EROFS_FS_ZIP */
199
200/* we strictly follow PAGE_SIZE and no buffer head yet */
201#define LOG_BLOCK_SIZE PAGE_SHIFT
202
203#undef LOG_SECTORS_PER_BLOCK
204#define LOG_SECTORS_PER_BLOCK (PAGE_SHIFT - 9)
205
206#undef SECTORS_PER_BLOCK
207#define SECTORS_PER_BLOCK (1 << SECTORS_PER_BLOCK)
208
209#define EROFS_BLKSIZ (1 << LOG_BLOCK_SIZE)
210
211#if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
212#error erofs cannot be used in this platform
213#endif
214
215#define ROOT_NID(sb) ((sb)->root_nid)
216
217#define erofs_blknr(addr) ((addr) / EROFS_BLKSIZ)
218#define erofs_blkoff(addr) ((addr) % EROFS_BLKSIZ)
219#define blknr_to_addr(nr) ((erofs_off_t)(nr) * EROFS_BLKSIZ)
220
221static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
222{
223 return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
224}
225
226#define EROFS_FEATURE_FUNCS(name, compat, feature) \
227static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
228{ \
229 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
230}
231
232EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING)
233EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
234EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
235EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
236
237/* atomic flag definitions */
238#define EROFS_I_EA_INITED_BIT 0
239#define EROFS_I_Z_INITED_BIT 1
240
241/* bitlock definitions (arranged in reverse order) */
242#define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
243#define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
244
245struct erofs_inode {
246 erofs_nid_t nid;
247
248 /* atomic flags (including bitlocks) */
249 unsigned long flags;
250
251 unsigned char datalayout;
252 unsigned char inode_isize;
253 unsigned short xattr_isize;
254
255 unsigned int xattr_shared_count;
256 unsigned int *xattr_shared_xattrs;
257
258 union {
259 erofs_blk_t raw_blkaddr;
260#ifdef CONFIG_EROFS_FS_ZIP
261 struct {
262 unsigned short z_advise;
263 unsigned char z_algorithmtype[2];
264 unsigned char z_logical_clusterbits;
265 };
266#endif /* CONFIG_EROFS_FS_ZIP */
267 };
268 /* the corresponding vfs inode */
269 struct inode vfs_inode;
270};
271
272#define EROFS_I(ptr) \
273 container_of(ptr, struct erofs_inode, vfs_inode)
274
275static inline unsigned long erofs_inode_datablocks(struct inode *inode)
276{
277 /* since i_size cannot be changed */
278 return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
279}
280
281static inline unsigned int erofs_bitrange(unsigned int value, unsigned int bit,
282 unsigned int bits)
283{
284
285 return (value >> bit) & ((1 << bits) - 1);
286}
287
288
289static inline unsigned int erofs_inode_version(unsigned int value)
290{
291 return erofs_bitrange(value, EROFS_I_VERSION_BIT,
292 EROFS_I_VERSION_BITS);
293}
294
295static inline unsigned int erofs_inode_datalayout(unsigned int value)
296{
297 return erofs_bitrange(value, EROFS_I_DATALAYOUT_BIT,
298 EROFS_I_DATALAYOUT_BITS);
299}
300
301extern const struct super_operations erofs_sops;
302
303extern const struct address_space_operations erofs_raw_access_aops;
304extern const struct address_space_operations z_erofs_aops;
305
306/*
307 * Logical to physical block mapping
308 *
309 * Different with other file systems, it is used for 2 access modes:
310 *
311 * 1) RAW access mode:
312 *
313 * Users pass a valid (m_lblk, m_lofs -- usually 0) pair,
314 * and get the valid m_pblk, m_pofs and the longest m_len(in bytes).
315 *
316 * Note that m_lblk in the RAW access mode refers to the number of
317 * the compressed ondisk block rather than the uncompressed
318 * in-memory block for the compressed file.
319 *
320 * m_pofs equals to m_lofs except for the inline data page.
321 *
322 * 2) Normal access mode:
323 *
324 * If the inode is not compressed, it has no difference with
325 * the RAW access mode. However, if the inode is compressed,
326 * users should pass a valid (m_lblk, m_lofs) pair, and get
327 * the needed m_pblk, m_pofs, m_len to get the compressed data
328 * and the updated m_lblk, m_lofs which indicates the start
329 * of the corresponding uncompressed data in the file.
330 */
331enum {
332 BH_Zipped = BH_PrivateStart,
333 BH_FullMapped,
334};
335
336/* Has a disk mapping */
337#define EROFS_MAP_MAPPED (1 << BH_Mapped)
338/* Located in metadata (could be copied from bd_inode) */
339#define EROFS_MAP_META (1 << BH_Meta)
340/* The extent has been compressed */
341#define EROFS_MAP_ZIPPED (1 << BH_Zipped)
342/* The length of extent is full */
343#define EROFS_MAP_FULL_MAPPED (1 << BH_FullMapped)
344
345struct erofs_map_blocks {
346 erofs_off_t m_pa, m_la;
347 u64 m_plen, m_llen;
348
349 unsigned int m_flags;
350
351 struct page *mpage;
352};
353
354/* Flags used by erofs_map_blocks_flatmode() */
355#define EROFS_GET_BLOCKS_RAW 0x0001
356
357/* zmap.c */
358#ifdef CONFIG_EROFS_FS_ZIP
359int z_erofs_fill_inode(struct inode *inode);
360int z_erofs_map_blocks_iter(struct inode *inode,
361 struct erofs_map_blocks *map,
362 int flags);
363#else
364static inline int z_erofs_fill_inode(struct inode *inode) { return -EOPNOTSUPP; }
365static inline int z_erofs_map_blocks_iter(struct inode *inode,
366 struct erofs_map_blocks *map,
367 int flags)
368{
369 return -EOPNOTSUPP;
370}
371#endif /* !CONFIG_EROFS_FS_ZIP */
372
373/* data.c */
374struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr);
375
376/* inode.c */
377static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
378{
379#if BITS_PER_LONG == 32
380 return (nid >> 32) ^ (nid & 0xffffffff);
381#else
382 return nid;
383#endif
384}
385
386extern const struct inode_operations erofs_generic_iops;
387extern const struct inode_operations erofs_symlink_iops;
388extern const struct inode_operations erofs_fast_symlink_iops;
389
390struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir);
391int erofs_getattr(struct user_namespace *mnt_userns, const struct path *path,
392 struct kstat *stat, u32 request_mask,
393 unsigned int query_flags);
394
395/* namei.c */
396extern const struct inode_operations erofs_dir_iops;
397
398int erofs_namei(struct inode *dir, struct qstr *name,
399 erofs_nid_t *nid, unsigned int *d_type);
400
401/* dir.c */
402extern const struct file_operations erofs_dir_fops;
403
404static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
405{
406 int retried = 0;
407
408 while (1) {
409 void *p = vm_map_ram(pages, count, -1);
410
411 /* retry two more times (totally 3 times) */
412 if (p || ++retried >= 3)
413 return p;
414 vm_unmap_aliases();
415 }
416 return NULL;
417}
418
419/* pcpubuf.c */
420void *erofs_get_pcpubuf(unsigned int requiredpages);
421void erofs_put_pcpubuf(void *ptr);
422int erofs_pcpubuf_growsize(unsigned int nrpages);
423void erofs_pcpubuf_init(void);
424void erofs_pcpubuf_exit(void);
425
426/* utils.c / zdata.c */
427struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp);
428
429#ifdef CONFIG_EROFS_FS_ZIP
430int erofs_workgroup_put(struct erofs_workgroup *grp);
431struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
432 pgoff_t index);
433struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
434 struct erofs_workgroup *grp);
435void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
436void erofs_shrinker_register(struct super_block *sb);
437void erofs_shrinker_unregister(struct super_block *sb);
438int __init erofs_init_shrinker(void);
439void erofs_exit_shrinker(void);
440int __init z_erofs_init_zip_subsystem(void);
441void z_erofs_exit_zip_subsystem(void);
442int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
443 struct erofs_workgroup *egrp);
444int erofs_try_to_free_cached_page(struct address_space *mapping,
445 struct page *page);
446int z_erofs_load_lz4_config(struct super_block *sb,
447 struct erofs_super_block *dsb,
448 struct z_erofs_lz4_cfgs *lz4, int len);
449#else
450static inline void erofs_shrinker_register(struct super_block *sb) {}
451static inline void erofs_shrinker_unregister(struct super_block *sb) {}
452static inline int erofs_init_shrinker(void) { return 0; }
453static inline void erofs_exit_shrinker(void) {}
454static inline int z_erofs_init_zip_subsystem(void) { return 0; }
455static inline void z_erofs_exit_zip_subsystem(void) {}
456static inline int z_erofs_load_lz4_config(struct super_block *sb,
457 struct erofs_super_block *dsb,
458 struct z_erofs_lz4_cfgs *lz4, int len)
459{
460 if (lz4 || dsb->u1.lz4_max_distance) {
461 erofs_err(sb, "lz4 algorithm isn't enabled");
462 return -EINVAL;
463 }
464 return 0;
465}
466#endif /* !CONFIG_EROFS_FS_ZIP */
467
468#define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
469
470#endif /* __EROFS_INTERNAL_H */