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