xref: /linux/fs/erofs/internal.h (revision 5e2cb28dd7e182dfa641550dfa225913509ad45d)
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 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 
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 #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 
79 struct erofs_dev_context {
80 	struct idr tree;
81 	struct rw_semaphore rwsem;
82 
83 	unsigned int extra_devices;
84 	bool flatdev;
85 };
86 
87 struct 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 */
95 struct 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 
102 struct erofs_domain {
103 	refcount_t ref;
104 	struct list_head list;
105 	struct fscache_volume *volume;
106 	char *domain_id;
107 };
108 
109 struct 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 
120 struct erofs_xattr_prefix_item {
121 	struct erofs_xattr_long_prefix *prefix;
122 	u8 infix_len;
123 };
124 
125 struct 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 
203 static inline bool erofs_is_fscache_mode(struct super_block *sb)
204 {
205 	return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && !sb->s_bdev;
206 }
207 
208 enum {
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 */
215 struct erofs_workgroup {
216 	pgoff_t index;
217 	struct lockref lockref;
218 };
219 
220 enum 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 
225 struct 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) \
239 static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
240 { \
241 	return sbi->feature_##compat & EROFS_FEATURE_##feature; \
242 }
243 
244 EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
245 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
246 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
247 EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
248 EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
249 EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
250 EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
251 EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
252 EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
253 EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
254 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
255 EROFS_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 
265 struct 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 
307 static 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 
315 static inline unsigned int erofs_inode_version(unsigned int ifmt)
316 {
317 	return (ifmt >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASK;
318 }
319 
320 static 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  */
329 static inline
330 struct 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 
351 struct 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 
372 enum {
373 	Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
374 	Z_EROFS_COMPRESSION_INTERLACED,
375 	Z_EROFS_COMPRESSION_RUNTIME_MAX
376 };
377 
378 struct 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 
388 extern struct file_system_type erofs_fs_type;
389 extern const struct super_operations erofs_sops;
390 
391 extern const struct address_space_operations erofs_raw_access_aops;
392 extern const struct address_space_operations z_erofs_aops;
393 extern const struct address_space_operations erofs_fscache_access_aops;
394 
395 extern const struct inode_operations erofs_generic_iops;
396 extern const struct inode_operations erofs_symlink_iops;
397 extern const struct inode_operations erofs_fast_symlink_iops;
398 extern const struct inode_operations erofs_dir_iops;
399 
400 extern const struct file_operations erofs_file_fops;
401 extern const struct file_operations erofs_dir_fops;
402 
403 extern 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 
409 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
410 			  erofs_off_t *offset, int *lengthp);
411 void erofs_unmap_metabuf(struct erofs_buf *buf);
412 void erofs_put_metabuf(struct erofs_buf *buf);
413 void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
414 		  enum erofs_kmap_type type);
415 void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
416 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
417 			 erofs_blk_t blkaddr, enum erofs_kmap_type type);
418 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
419 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
420 		 u64 start, u64 len);
421 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
422 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
423 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
424 		  struct kstat *stat, u32 request_mask,
425 		  unsigned int query_flags);
426 int erofs_namei(struct inode *dir, const struct qstr *name,
427 		erofs_nid_t *nid, unsigned int *d_type);
428 
429 static 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 
444 int erofs_register_sysfs(struct super_block *sb);
445 void erofs_unregister_sysfs(struct super_block *sb);
446 int __init erofs_init_sysfs(void);
447 void erofs_exit_sysfs(void);
448 
449 struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
450 static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
451 {
452 	set_page_private(page, (unsigned long)*pagepool);
453 	*pagepool = page;
454 }
455 void erofs_release_pages(struct page **pagepool);
456 
457 #ifdef CONFIG_EROFS_FS_ZIP
458 void erofs_workgroup_put(struct erofs_workgroup *grp);
459 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
460 					     pgoff_t index);
461 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
462 					       struct erofs_workgroup *grp);
463 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
464 void erofs_shrinker_register(struct super_block *sb);
465 void erofs_shrinker_unregister(struct super_block *sb);
466 int __init erofs_init_shrinker(void);
467 void erofs_exit_shrinker(void);
468 int __init z_erofs_init_zip_subsystem(void);
469 void z_erofs_exit_zip_subsystem(void);
470 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
471 				       struct erofs_workgroup *egrp);
472 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
473 			    int flags);
474 void *erofs_get_pcpubuf(unsigned int requiredpages);
475 void erofs_put_pcpubuf(void *ptr);
476 int erofs_pcpubuf_growsize(unsigned int nrpages);
477 void __init erofs_pcpubuf_init(void);
478 void erofs_pcpubuf_exit(void);
479 int erofs_init_managed_cache(struct super_block *sb);
480 int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb);
481 #else
482 static inline void erofs_shrinker_register(struct super_block *sb) {}
483 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
484 static inline int erofs_init_shrinker(void) { return 0; }
485 static inline void erofs_exit_shrinker(void) {}
486 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
487 static inline void z_erofs_exit_zip_subsystem(void) {}
488 static inline void erofs_pcpubuf_init(void) {}
489 static inline void erofs_pcpubuf_exit(void) {}
490 static 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
494 int __init z_erofs_lzma_init(void);
495 void z_erofs_lzma_exit(void);
496 #else
497 static inline int z_erofs_lzma_init(void) { return 0; }
498 static inline int z_erofs_lzma_exit(void) { return 0; }
499 #endif	/* !CONFIG_EROFS_FS_ZIP_LZMA */
500 
501 #ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
502 int __init z_erofs_deflate_init(void);
503 void z_erofs_deflate_exit(void);
504 #else
505 static inline int z_erofs_deflate_init(void) { return 0; }
506 static inline int z_erofs_deflate_exit(void) { return 0; }
507 #endif	/* !CONFIG_EROFS_FS_ZIP_DEFLATE */
508 
509 #ifdef CONFIG_EROFS_FS_ONDEMAND
510 int erofs_fscache_register_fs(struct super_block *sb);
511 void erofs_fscache_unregister_fs(struct super_block *sb);
512 
513 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
514 					char *name, unsigned int flags);
515 void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
516 #else
517 static inline int erofs_fscache_register_fs(struct super_block *sb)
518 {
519 	return -EOPNOTSUPP;
520 }
521 static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
522 
523 static inline
524 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
525 					char *name, unsigned int flags)
526 {
527 	return ERR_PTR(-EOPNOTSUPP);
528 }
529 
530 static 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 */
538