xref: /linux/fs/erofs/internal.h (revision 1553a1c48281243359a9529a10ddb551f3b967ab)
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 *bdev_file;
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 const struct super_operations erofs_sops;
389 
390 extern const struct address_space_operations erofs_raw_access_aops;
391 extern const struct address_space_operations z_erofs_aops;
392 extern const struct address_space_operations erofs_fscache_access_aops;
393 
394 extern const struct inode_operations erofs_generic_iops;
395 extern const struct inode_operations erofs_symlink_iops;
396 extern const struct inode_operations erofs_fast_symlink_iops;
397 extern const struct inode_operations erofs_dir_iops;
398 
399 extern const struct file_operations erofs_file_fops;
400 extern const struct file_operations erofs_dir_fops;
401 
402 extern const struct iomap_ops z_erofs_iomap_report_ops;
403 
404 /* flags for erofs_fscache_register_cookie() */
405 #define EROFS_REG_COOKIE_SHARE		0x0001
406 #define EROFS_REG_COOKIE_NEED_NOEXIST	0x0002
407 
408 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
409 			  erofs_off_t *offset, int *lengthp);
410 void erofs_unmap_metabuf(struct erofs_buf *buf);
411 void erofs_put_metabuf(struct erofs_buf *buf);
412 void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
413 		  enum erofs_kmap_type type);
414 void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
415 void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
416 			 erofs_blk_t blkaddr, enum erofs_kmap_type type);
417 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
418 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
419 		 u64 start, u64 len);
420 int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
421 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
422 int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
423 		  struct kstat *stat, u32 request_mask,
424 		  unsigned int query_flags);
425 int erofs_namei(struct inode *dir, const struct qstr *name,
426 		erofs_nid_t *nid, unsigned int *d_type);
427 
428 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
429 {
430 	int retried = 0;
431 
432 	while (1) {
433 		void *p = vm_map_ram(pages, count, -1);
434 
435 		/* retry two more times (totally 3 times) */
436 		if (p || ++retried >= 3)
437 			return p;
438 		vm_unmap_aliases();
439 	}
440 	return NULL;
441 }
442 
443 int erofs_register_sysfs(struct super_block *sb);
444 void erofs_unregister_sysfs(struct super_block *sb);
445 int __init erofs_init_sysfs(void);
446 void erofs_exit_sysfs(void);
447 
448 struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
449 static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
450 {
451 	set_page_private(page, (unsigned long)*pagepool);
452 	*pagepool = page;
453 }
454 void erofs_release_pages(struct page **pagepool);
455 
456 #ifdef CONFIG_EROFS_FS_ZIP
457 void erofs_workgroup_put(struct erofs_workgroup *grp);
458 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
459 					     pgoff_t index);
460 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
461 					       struct erofs_workgroup *grp);
462 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
463 void erofs_shrinker_register(struct super_block *sb);
464 void erofs_shrinker_unregister(struct super_block *sb);
465 int __init erofs_init_shrinker(void);
466 void erofs_exit_shrinker(void);
467 int __init z_erofs_init_zip_subsystem(void);
468 void z_erofs_exit_zip_subsystem(void);
469 int erofs_try_to_free_all_cached_folios(struct erofs_sb_info *sbi,
470 					struct erofs_workgroup *egrp);
471 int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
472 			    int flags);
473 void *erofs_get_pcpubuf(unsigned int requiredpages);
474 void erofs_put_pcpubuf(void *ptr);
475 int erofs_pcpubuf_growsize(unsigned int nrpages);
476 void __init erofs_pcpubuf_init(void);
477 void erofs_pcpubuf_exit(void);
478 int erofs_init_managed_cache(struct super_block *sb);
479 int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb);
480 #else
481 static inline void erofs_shrinker_register(struct super_block *sb) {}
482 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
483 static inline int erofs_init_shrinker(void) { return 0; }
484 static inline void erofs_exit_shrinker(void) {}
485 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
486 static inline void z_erofs_exit_zip_subsystem(void) {}
487 static inline void erofs_pcpubuf_init(void) {}
488 static inline void erofs_pcpubuf_exit(void) {}
489 static inline int erofs_init_managed_cache(struct super_block *sb) { return 0; }
490 #endif	/* !CONFIG_EROFS_FS_ZIP */
491 
492 #ifdef CONFIG_EROFS_FS_ZIP_LZMA
493 int __init z_erofs_lzma_init(void);
494 void z_erofs_lzma_exit(void);
495 #else
496 static inline int z_erofs_lzma_init(void) { return 0; }
497 static inline int z_erofs_lzma_exit(void) { return 0; }
498 #endif	/* !CONFIG_EROFS_FS_ZIP_LZMA */
499 
500 #ifdef CONFIG_EROFS_FS_ZIP_DEFLATE
501 int __init z_erofs_deflate_init(void);
502 void z_erofs_deflate_exit(void);
503 #else
504 static inline int z_erofs_deflate_init(void) { return 0; }
505 static inline int z_erofs_deflate_exit(void) { return 0; }
506 #endif	/* !CONFIG_EROFS_FS_ZIP_DEFLATE */
507 
508 #ifdef CONFIG_EROFS_FS_ONDEMAND
509 int erofs_fscache_register_fs(struct super_block *sb);
510 void erofs_fscache_unregister_fs(struct super_block *sb);
511 
512 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
513 					char *name, unsigned int flags);
514 void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
515 struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev);
516 void erofs_fscache_submit_bio(struct bio *bio);
517 #else
518 static inline int erofs_fscache_register_fs(struct super_block *sb)
519 {
520 	return -EOPNOTSUPP;
521 }
522 static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
523 
524 static inline
525 struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
526 					char *name, unsigned int flags)
527 {
528 	return ERR_PTR(-EOPNOTSUPP);
529 }
530 
531 static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
532 {
533 }
534 static inline struct bio *erofs_fscache_bio_alloc(struct erofs_map_dev *mdev) { return NULL; }
535 static inline void erofs_fscache_submit_bio(struct bio *bio) {}
536 #endif
537 
538 #define EFSCORRUPTED    EUCLEAN         /* Filesystem is corrupted */
539 
540 #endif	/* __EROFS_INTERNAL_H */
541