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