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 
38 typedef u64 erofs_nid_t;
39 typedef u64 erofs_off_t;
40 typedef u64 erofs_blk_t;
41 
42 struct 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 
53 enum {
54 	EROFS_SYNC_DECOMPRESS_AUTO,
55 	EROFS_SYNC_DECOMPRESS_FORCE_ON,
56 	EROFS_SYNC_DECOMPRESS_FORCE_OFF
57 };
58 
59 struct 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 
69 struct 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 */
78 struct 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 
85 struct erofs_domain {
86 	refcount_t ref;
87 	struct list_head list;
88 	struct fscache_volume *volume;
89 	char *domain_id;
90 };
91 
92 struct 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 
103 struct erofs_xattr_prefix_item {
104 	struct erofs_xattr_long_prefix *prefix;
105 	u8 infix_len;
106 };
107 
108 struct 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 erofs_dev_context *devs;
129 	u64 total_blocks;
130 
131 	u32 meta_blkaddr;
132 #ifdef CONFIG_EROFS_FS_XATTR
133 	u32 xattr_blkaddr;
134 	u32 xattr_prefix_start;
135 	u8 xattr_prefix_count;
136 	struct erofs_xattr_prefix_item *xattr_prefixes;
137 	unsigned int xattr_filter_reserved;
138 #endif
139 	u16 device_id_mask;	/* valid bits of device id to be used */
140 
141 	unsigned char islotbits;	/* inode slot unit size in bit shift */
142 	unsigned char blkszbits;	/* filesystem block size in bit shift */
143 
144 	u32 sb_size;			/* total superblock size */
145 	u32 fixed_nsec;
146 	s64 epoch;
147 
148 	/* what we really care is nid, rather than ino.. */
149 	erofs_nid_t root_nid;
150 	erofs_nid_t packed_nid;
151 	/* used for statfs, f_files - f_favail */
152 	u64 inos;
153 
154 	u32 feature_compat;
155 	u32 feature_incompat;
156 
157 	/* sysfs support */
158 	struct kobject s_kobj;		/* /sys/fs/erofs/<devname> */
159 	struct completion s_kobj_unregister;
160 
161 	/* fscache support */
162 	struct fscache_volume *volume;
163 	struct erofs_domain *domain;
164 	char *fsid;
165 	char *domain_id;
166 };
167 
168 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
169 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
170 
171 /* Mount flags set via mount options or defaults */
172 #define EROFS_MOUNT_XATTR_USER		0x00000010
173 #define EROFS_MOUNT_POSIX_ACL		0x00000020
174 #define EROFS_MOUNT_DAX_ALWAYS		0x00000040
175 #define EROFS_MOUNT_DAX_NEVER		0x00000080
176 #define EROFS_MOUNT_DIRECT_IO		0x00000100
177 
178 #define clear_opt(opt, option)	((opt)->mount_opt &= ~EROFS_MOUNT_##option)
179 #define set_opt(opt, option)	((opt)->mount_opt |= EROFS_MOUNT_##option)
180 #define test_opt(opt, option)	((opt)->mount_opt & EROFS_MOUNT_##option)
181 
182 static inline bool erofs_is_fileio_mode(struct erofs_sb_info *sbi)
183 {
184 	return IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) && sbi->dif0.file;
185 }
186 
187 static inline bool erofs_is_fscache_mode(struct super_block *sb)
188 {
189 	return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) &&
190 			!erofs_is_fileio_mode(EROFS_SB(sb)) && !sb->s_bdev;
191 }
192 
193 enum {
194 	EROFS_ZIP_CACHE_DISABLED,
195 	EROFS_ZIP_CACHE_READAHEAD,
196 	EROFS_ZIP_CACHE_READAROUND
197 };
198 
199 struct erofs_buf {
200 	struct address_space *mapping;
201 	struct file *file;
202 	u64 off;
203 	struct page *page;
204 	void *base;
205 };
206 #define __EROFS_BUF_INITIALIZER	((struct erofs_buf){ .page = NULL })
207 
208 #define erofs_blknr(sb, pos)	((erofs_blk_t)((pos) >> (sb)->s_blocksize_bits))
209 #define erofs_blkoff(sb, pos)	((pos) & ((sb)->s_blocksize - 1))
210 #define erofs_pos(sb, blk)	((erofs_off_t)(blk) << (sb)->s_blocksize_bits)
211 #define erofs_iblks(i)	(round_up((i)->i_size, i_blocksize(i)) >> (i)->i_blkbits)
212 
213 #define EROFS_FEATURE_FUNCS(name, compat, feature) \
214 static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
215 { \
216 	return sbi->feature_##compat & EROFS_FEATURE_##feature; \
217 }
218 
219 EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
220 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
221 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
222 EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
223 EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
224 EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
225 EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
226 EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
227 EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
228 EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
229 EROFS_FEATURE_FUNCS(48bit, incompat, INCOMPAT_48BIT)
230 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
231 EROFS_FEATURE_FUNCS(xattr_filter, compat, COMPAT_XATTR_FILTER)
232 
233 /* atomic flag definitions */
234 #define EROFS_I_EA_INITED_BIT	0
235 #define EROFS_I_Z_INITED_BIT	1
236 
237 /* bitlock definitions (arranged in reverse order) */
238 #define EROFS_I_BL_XATTR_BIT	(BITS_PER_LONG - 1)
239 #define EROFS_I_BL_Z_BIT	(BITS_PER_LONG - 2)
240 
241 struct erofs_inode {
242 	erofs_nid_t nid;
243 
244 	/* atomic flags (including bitlocks) */
245 	unsigned long flags;
246 
247 	unsigned char datalayout;
248 	unsigned char inode_isize;
249 	bool dot_omitted;
250 	unsigned int xattr_isize;
251 
252 	unsigned int xattr_name_filter;
253 	unsigned int xattr_shared_count;
254 	unsigned int *xattr_shared_xattrs;
255 
256 	union {
257 		erofs_blk_t startblk;
258 		struct {
259 			unsigned short	chunkformat;
260 			unsigned char	chunkbits;
261 		};
262 #ifdef CONFIG_EROFS_FS_ZIP
263 		struct {
264 			unsigned short z_advise;
265 			unsigned char  z_algorithmtype[2];
266 			unsigned char  z_lclusterbits;
267 			union {
268 				u64    z_tailextent_headlcn;
269 				u64    z_extents;
270 			};
271 			erofs_off_t    z_fragmentoff;
272 			unsigned short z_idata_size;
273 		};
274 #endif	/* CONFIG_EROFS_FS_ZIP */
275 	};
276 	/* the corresponding vfs inode */
277 	struct inode vfs_inode;
278 };
279 
280 #define EROFS_I(ptr)	container_of(ptr, struct erofs_inode, vfs_inode)
281 
282 static inline erofs_off_t erofs_iloc(struct inode *inode)
283 {
284 	struct erofs_sb_info *sbi = EROFS_I_SB(inode);
285 
286 	return erofs_pos(inode->i_sb, sbi->meta_blkaddr) +
287 		(EROFS_I(inode)->nid << sbi->islotbits);
288 }
289 
290 static inline unsigned int erofs_inode_version(unsigned int ifmt)
291 {
292 	return (ifmt >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASK;
293 }
294 
295 static inline unsigned int erofs_inode_datalayout(unsigned int ifmt)
296 {
297 	return (ifmt >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASK;
298 }
299 
300 /* reclaiming is never triggered when allocating new folios. */
301 static inline struct folio *erofs_grab_folio_nowait(struct address_space *as,
302 						    pgoff_t index)
303 {
304 	return __filemap_get_folio(as, index,
305 			FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
306 			readahead_gfp_mask(as) & ~__GFP_RECLAIM);
307 }
308 
309 /* Has a disk mapping */
310 #define EROFS_MAP_MAPPED	0x0001
311 /* Located in metadata (could be copied from bd_inode) */
312 #define EROFS_MAP_META		0x0002
313 /* The extent is encoded */
314 #define EROFS_MAP_ENCODED	0x0004
315 /* The length of extent is full */
316 #define EROFS_MAP_FULL_MAPPED	0x0008
317 /* Located in the special packed inode */
318 #define EROFS_MAP_FRAGMENT	0x0010
319 /* The extent refers to partial decompressed data */
320 #define EROFS_MAP_PARTIAL_REF	0x0020
321 
322 struct erofs_map_blocks {
323 	struct erofs_buf buf;
324 
325 	erofs_off_t m_pa, m_la;
326 	u64 m_plen, m_llen;
327 
328 	unsigned short m_deviceid;
329 	char m_algorithmformat;
330 	unsigned int m_flags;
331 };
332 
333 /*
334  * Used to get the exact decompressed length, e.g. fiemap (consider lookback
335  * approach instead if possible since it's more metadata lightweight.)
336  */
337 #define EROFS_GET_BLOCKS_FIEMAP		0x0001
338 /* Used to map the whole extent if non-negligible data is requested for LZMA */
339 #define EROFS_GET_BLOCKS_READMORE	0x0002
340 /* Used to map tail extent for tailpacking inline or fragment pcluster */
341 #define EROFS_GET_BLOCKS_FINDTAIL	0x0004
342 
343 enum {
344 	Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
345 	Z_EROFS_COMPRESSION_INTERLACED,
346 	Z_EROFS_COMPRESSION_RUNTIME_MAX
347 };
348 
349 struct erofs_map_dev {
350 	struct super_block *m_sb;
351 	struct erofs_device_info *m_dif;
352 	struct block_device *m_bdev;
353 
354 	erofs_off_t m_pa;
355 	unsigned int m_deviceid;
356 };
357 
358 extern const struct super_operations erofs_sops;
359 
360 extern const struct address_space_operations erofs_aops;
361 extern const struct address_space_operations erofs_fileio_aops;
362 extern const struct address_space_operations z_erofs_aops;
363 extern const struct address_space_operations erofs_fscache_access_aops;
364 
365 extern const struct inode_operations erofs_generic_iops;
366 extern const struct inode_operations erofs_symlink_iops;
367 extern const struct inode_operations erofs_fast_symlink_iops;
368 extern const struct inode_operations erofs_dir_iops;
369 
370 extern const struct file_operations erofs_file_fops;
371 extern const struct file_operations erofs_dir_fops;
372 
373 extern const struct iomap_ops z_erofs_iomap_report_ops;
374 
375 /* flags for erofs_fscache_register_cookie() */
376 #define EROFS_REG_COOKIE_SHARE		0x0001
377 #define EROFS_REG_COOKIE_NEED_NOEXIST	0x0002
378 
379 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
380 			  erofs_off_t *offset, int *lengthp);
381 void erofs_unmap_metabuf(struct erofs_buf *buf);
382 void erofs_put_metabuf(struct erofs_buf *buf);
383 void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap);
384 void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
385 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
386 			 erofs_off_t offset, bool need_kmap);
387 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
388 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
389 		 u64 start, u64 len);
390 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
391 void erofs_onlinefolio_init(struct folio *folio);
392 void erofs_onlinefolio_split(struct folio *folio);
393 void erofs_onlinefolio_end(struct folio *folio, int err);
394 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
395 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
396 		  struct kstat *stat, u32 request_mask,
397 		  unsigned int query_flags);
398 int erofs_namei(struct inode *dir, const struct qstr *name,
399 		erofs_nid_t *nid, unsigned int *d_type);
400 
401 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
402 {
403 	int retried = 0;
404 
405 	while (1) {
406 		void *p = vm_map_ram(pages, count, -1);
407 
408 		/* retry two more times (totally 3 times) */
409 		if (p || ++retried >= 3)
410 			return p;
411 		vm_unmap_aliases();
412 	}
413 	return NULL;
414 }
415 
416 int erofs_register_sysfs(struct super_block *sb);
417 void erofs_unregister_sysfs(struct super_block *sb);
418 int __init erofs_init_sysfs(void);
419 void erofs_exit_sysfs(void);
420 
421 struct page *__erofs_allocpage(struct page **pagepool, gfp_t gfp, bool tryrsv);
422 static inline struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp)
423 {
424 	return __erofs_allocpage(pagepool, gfp, false);
425 }
426 static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
427 {
428 	set_page_private(page, (unsigned long)*pagepool);
429 	*pagepool = page;
430 }
431 void erofs_release_pages(struct page **pagepool);
432 
433 #ifdef CONFIG_EROFS_FS_ZIP
434 #define MNGD_MAPPING(sbi)	((sbi)->managed_cache->i_mapping)
435 
436 extern atomic_long_t erofs_global_shrink_cnt;
437 void erofs_shrinker_register(struct super_block *sb);
438 void erofs_shrinker_unregister(struct super_block *sb);
439 int __init erofs_init_shrinker(void);
440 void erofs_exit_shrinker(void);
441 int __init z_erofs_init_subsystem(void);
442 void z_erofs_exit_subsystem(void);
443 int z_erofs_init_super(struct super_block *sb);
444 unsigned long z_erofs_shrink_scan(struct erofs_sb_info *sbi,
445 				  unsigned long nr_shrink);
446 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
447 			    int flags);
448 void *z_erofs_get_gbuf(unsigned int requiredpages);
449 void z_erofs_put_gbuf(void *ptr);
450 int z_erofs_gbuf_growsize(unsigned int nrpages);
451 int __init z_erofs_gbuf_init(void);
452 void z_erofs_gbuf_exit(void);
453 int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb);
454 #else
455 static inline void erofs_shrinker_register(struct super_block *sb) {}
456 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
457 static inline int erofs_init_shrinker(void) { return 0; }
458 static inline void erofs_exit_shrinker(void) {}
459 static inline int z_erofs_init_subsystem(void) { return 0; }
460 static inline void z_erofs_exit_subsystem(void) {}
461 static inline int z_erofs_init_super(struct super_block *sb) { return 0; }
462 #endif	/* !CONFIG_EROFS_FS_ZIP */
463 
464 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
465 struct bio *erofs_fileio_bio_alloc(struct erofs_map_dev *mdev);
466 void erofs_fileio_submit_bio(struct bio *bio);
467 #else
468 static inline struct bio *erofs_fileio_bio_alloc(struct erofs_map_dev *mdev) { return NULL; }
469 static inline void erofs_fileio_submit_bio(struct bio *bio) {}
470 #endif
471 
472 #ifdef CONFIG_EROFS_FS_ONDEMAND
473 int erofs_fscache_register_fs(struct super_block *sb);
474 void erofs_fscache_unregister_fs(struct super_block *sb);
475 
476 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
477 					char *name, unsigned int flags);
478 void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
479 struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev);
480 void erofs_fscache_submit_bio(struct bio *bio);
481 #else
482 static inline int erofs_fscache_register_fs(struct super_block *sb)
483 {
484 	return -EOPNOTSUPP;
485 }
486 static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
487 
488 static inline
489 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
490 					char *name, unsigned int flags)
491 {
492 	return ERR_PTR(-EOPNOTSUPP);
493 }
494 
495 static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
496 {
497 }
498 static inline struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev) { return NULL; }
499 static inline void erofs_fscache_submit_bio(struct bio *bio) {}
500 #endif
501 
502 #define EFSCORRUPTED    EUCLEAN         /* Filesystem is corrupted */
503 
504 #endif	/* __EROFS_INTERNAL_H */
505