xref: /openbmc/linux/fs/erofs/internal.h (revision 8d8a09b0)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2017-2018 HUAWEI, Inc.
4  *             http://www.huawei.com/
5  * Created by Gao Xiang <gaoxiang25@huawei.com>
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/buffer_head.h>
16 #include <linux/magic.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.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 #define errln(x, ...)   pr_err(x "\n", ##__VA_ARGS__)
26 #define infoln(x, ...)  pr_info(x "\n", ##__VA_ARGS__)
27 #ifdef CONFIG_EROFS_FS_DEBUG
28 #define debugln(x, ...) pr_debug(x "\n", ##__VA_ARGS__)
29 #define DBG_BUGON               BUG_ON
30 #else
31 #define debugln(x, ...)         ((void)0)
32 #define DBG_BUGON(x)            ((void)(x))
33 #endif	/* !CONFIG_EROFS_FS_DEBUG */
34 
35 enum {
36 	FAULT_KMALLOC,
37 	FAULT_READ_IO,
38 	FAULT_MAX,
39 };
40 
41 #ifdef CONFIG_EROFS_FAULT_INJECTION
42 extern const char *erofs_fault_name[FAULT_MAX];
43 #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
44 
45 struct erofs_fault_info {
46 	atomic_t inject_ops;
47 	unsigned int inject_rate;
48 	unsigned int inject_type;
49 };
50 #endif	/* CONFIG_EROFS_FAULT_INJECTION */
51 
52 /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
53 #define EROFS_SUPER_MAGIC   EROFS_SUPER_MAGIC_V1
54 
55 typedef u64 erofs_nid_t;
56 typedef u64 erofs_off_t;
57 /* data type for filesystem-wide blocks number */
58 typedef u32 erofs_blk_t;
59 
60 struct erofs_sb_info {
61 #ifdef CONFIG_EROFS_FS_ZIP
62 	/* list for all registered superblocks, mainly for shrinker */
63 	struct list_head list;
64 	struct mutex umount_mutex;
65 
66 	/* the dedicated workstation for compression */
67 	struct radix_tree_root workstn_tree;
68 
69 	/* threshold for decompression synchronously */
70 	unsigned int max_sync_decompress_pages;
71 
72 	unsigned int shrinker_run_no;
73 
74 	/* current strategy of how to use managed cache */
75 	unsigned char cache_strategy;
76 
77 	/* pseudo inode to manage cached pages */
78 	struct inode *managed_cache;
79 #endif	/* CONFIG_EROFS_FS_ZIP */
80 	u32 blocks;
81 	u32 meta_blkaddr;
82 #ifdef CONFIG_EROFS_FS_XATTR
83 	u32 xattr_blkaddr;
84 #endif
85 
86 	/* inode slot unit size in bit shift */
87 	unsigned char islotbits;
88 
89 	u32 build_time_nsec;
90 	u64 build_time;
91 
92 	/* what we really care is nid, rather than ino.. */
93 	erofs_nid_t root_nid;
94 	/* used for statfs, f_files - f_favail */
95 	u64 inos;
96 
97 	u8 uuid[16];                    /* 128-bit uuid for volume */
98 	u8 volume_name[16];             /* volume name */
99 	u32 requirements;
100 
101 	unsigned int mount_opt;
102 
103 #ifdef CONFIG_EROFS_FAULT_INJECTION
104 	struct erofs_fault_info fault_info;	/* For fault injection */
105 #endif
106 };
107 
108 #ifdef CONFIG_EROFS_FAULT_INJECTION
109 #define erofs_show_injection_info(type)					\
110 	infoln("inject %s in %s of %pS", erofs_fault_name[type],        \
111 		__func__, __builtin_return_address(0))
112 
113 static inline bool time_to_inject(struct erofs_sb_info *sbi, int type)
114 {
115 	struct erofs_fault_info *ffi = &sbi->fault_info;
116 
117 	if (!ffi->inject_rate)
118 		return false;
119 
120 	if (!IS_FAULT_SET(ffi, type))
121 		return false;
122 
123 	atomic_inc(&ffi->inject_ops);
124 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
125 		atomic_set(&ffi->inject_ops, 0);
126 		return true;
127 	}
128 	return false;
129 }
130 #else
131 static inline bool time_to_inject(struct erofs_sb_info *sbi, int type)
132 {
133 	return false;
134 }
135 
136 static inline void erofs_show_injection_info(int type)
137 {
138 }
139 #endif	/* !CONFIG_EROFS_FAULT_INJECTION */
140 
141 static inline void *erofs_kmalloc(struct erofs_sb_info *sbi,
142 					size_t size, gfp_t flags)
143 {
144 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
145 		erofs_show_injection_info(FAULT_KMALLOC);
146 		return NULL;
147 	}
148 	return kmalloc(size, flags);
149 }
150 
151 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
152 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
153 
154 /* Mount flags set via mount options or defaults */
155 #define EROFS_MOUNT_XATTR_USER		0x00000010
156 #define EROFS_MOUNT_POSIX_ACL		0x00000020
157 #define EROFS_MOUNT_FAULT_INJECTION	0x00000040
158 
159 #define clear_opt(sbi, option)	((sbi)->mount_opt &= ~EROFS_MOUNT_##option)
160 #define set_opt(sbi, option)	((sbi)->mount_opt |= EROFS_MOUNT_##option)
161 #define test_opt(sbi, option)	((sbi)->mount_opt & EROFS_MOUNT_##option)
162 
163 #ifdef CONFIG_EROFS_FS_ZIP
164 enum {
165 	EROFS_ZIP_CACHE_DISABLED,
166 	EROFS_ZIP_CACHE_READAHEAD,
167 	EROFS_ZIP_CACHE_READAROUND
168 };
169 
170 #define EROFS_LOCKED_MAGIC     (INT_MIN | 0xE0F510CCL)
171 
172 /* basic unit of the workstation of a super_block */
173 struct erofs_workgroup {
174 	/* the workgroup index in the workstation */
175 	pgoff_t index;
176 
177 	/* overall workgroup reference count */
178 	atomic_t refcount;
179 };
180 
181 #if defined(CONFIG_SMP)
182 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
183 						 int val)
184 {
185 	preempt_disable();
186 	if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
187 		preempt_enable();
188 		return false;
189 	}
190 	return true;
191 }
192 
193 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
194 					    int orig_val)
195 {
196 	/*
197 	 * other observers should notice all modifications
198 	 * in the freezing period.
199 	 */
200 	smp_mb();
201 	atomic_set(&grp->refcount, orig_val);
202 	preempt_enable();
203 }
204 
205 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
206 {
207 	return atomic_cond_read_relaxed(&grp->refcount,
208 					VAL != EROFS_LOCKED_MAGIC);
209 }
210 #else
211 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
212 						 int val)
213 {
214 	preempt_disable();
215 	/* no need to spin on UP platforms, let's just disable preemption. */
216 	if (val != atomic_read(&grp->refcount)) {
217 		preempt_enable();
218 		return false;
219 	}
220 	return true;
221 }
222 
223 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
224 					    int orig_val)
225 {
226 	preempt_enable();
227 }
228 
229 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
230 {
231 	int v = atomic_read(&grp->refcount);
232 
233 	/* workgroup is never freezed on uniprocessor systems */
234 	DBG_BUGON(v == EROFS_LOCKED_MAGIC);
235 	return v;
236 }
237 #endif	/* !CONFIG_SMP */
238 
239 /* hard limit of pages per compressed cluster */
240 #define Z_EROFS_CLUSTER_MAX_PAGES       (CONFIG_EROFS_FS_CLUSTER_PAGE_LIMIT)
241 #define EROFS_PCPUBUF_NR_PAGES          Z_EROFS_CLUSTER_MAX_PAGES
242 #else
243 #define EROFS_PCPUBUF_NR_PAGES          0
244 #endif	/* !CONFIG_EROFS_FS_ZIP */
245 
246 /* we strictly follow PAGE_SIZE and no buffer head yet */
247 #define LOG_BLOCK_SIZE		PAGE_SHIFT
248 
249 #undef LOG_SECTORS_PER_BLOCK
250 #define LOG_SECTORS_PER_BLOCK	(PAGE_SHIFT - 9)
251 
252 #undef SECTORS_PER_BLOCK
253 #define SECTORS_PER_BLOCK	(1 << SECTORS_PER_BLOCK)
254 
255 #define EROFS_BLKSIZ		(1 << LOG_BLOCK_SIZE)
256 
257 #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
258 #error erofs cannot be used in this platform
259 #endif
260 
261 #define EROFS_IO_MAX_RETRIES_NOFAIL     5
262 
263 #define ROOT_NID(sb)		((sb)->root_nid)
264 
265 #define erofs_blknr(addr)       ((addr) / EROFS_BLKSIZ)
266 #define erofs_blkoff(addr)      ((addr) % EROFS_BLKSIZ)
267 #define blknr_to_addr(nr)       ((erofs_off_t)(nr) * EROFS_BLKSIZ)
268 
269 static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
270 {
271 	return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
272 }
273 
274 /* atomic flag definitions */
275 #define EROFS_V_EA_INITED_BIT	0
276 #define EROFS_V_Z_INITED_BIT	1
277 
278 /* bitlock definitions (arranged in reverse order) */
279 #define EROFS_V_BL_XATTR_BIT	(BITS_PER_LONG - 1)
280 #define EROFS_V_BL_Z_BIT	(BITS_PER_LONG - 2)
281 
282 struct erofs_vnode {
283 	erofs_nid_t nid;
284 
285 	/* atomic flags (including bitlocks) */
286 	unsigned long flags;
287 
288 	unsigned char datamode;
289 	unsigned char inode_isize;
290 	unsigned short xattr_isize;
291 
292 	unsigned int xattr_shared_count;
293 	unsigned int *xattr_shared_xattrs;
294 
295 	union {
296 		erofs_blk_t raw_blkaddr;
297 #ifdef CONFIG_EROFS_FS_ZIP
298 		struct {
299 			unsigned short z_advise;
300 			unsigned char  z_algorithmtype[2];
301 			unsigned char  z_logical_clusterbits;
302 			unsigned char  z_physical_clusterbits[2];
303 		};
304 #endif	/* CONFIG_EROFS_FS_ZIP */
305 	};
306 	/* the corresponding vfs inode */
307 	struct inode vfs_inode;
308 };
309 
310 #define EROFS_V(ptr)	\
311 	container_of(ptr, struct erofs_vnode, vfs_inode)
312 
313 #define __inode_advise(x, bit, bits) \
314 	(((x) >> (bit)) & ((1 << (bits)) - 1))
315 
316 #define __inode_version(advise)	\
317 	__inode_advise(advise, EROFS_I_VERSION_BIT,	\
318 		EROFS_I_VERSION_BITS)
319 
320 #define __inode_data_mapping(advise)	\
321 	__inode_advise(advise, EROFS_I_DATA_MAPPING_BIT,\
322 		EROFS_I_DATA_MAPPING_BITS)
323 
324 static inline unsigned long inode_datablocks(struct inode *inode)
325 {
326 	/* since i_size cannot be changed */
327 	return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
328 }
329 
330 static inline bool is_inode_layout_compression(struct inode *inode)
331 {
332 	return erofs_inode_is_data_compressed(EROFS_V(inode)->datamode);
333 }
334 
335 static inline bool is_inode_flat_inline(struct inode *inode)
336 {
337 	return EROFS_V(inode)->datamode == EROFS_INODE_FLAT_INLINE;
338 }
339 
340 extern const struct super_operations erofs_sops;
341 
342 extern const struct address_space_operations erofs_raw_access_aops;
343 #ifdef CONFIG_EROFS_FS_ZIP
344 extern const struct address_space_operations z_erofs_vle_normalaccess_aops;
345 #endif
346 
347 /*
348  * Logical to physical block mapping, used by erofs_map_blocks()
349  *
350  * Different with other file systems, it is used for 2 access modes:
351  *
352  * 1) RAW access mode:
353  *
354  * Users pass a valid (m_lblk, m_lofs -- usually 0) pair,
355  * and get the valid m_pblk, m_pofs and the longest m_len(in bytes).
356  *
357  * Note that m_lblk in the RAW access mode refers to the number of
358  * the compressed ondisk block rather than the uncompressed
359  * in-memory block for the compressed file.
360  *
361  * m_pofs equals to m_lofs except for the inline data page.
362  *
363  * 2) Normal access mode:
364  *
365  * If the inode is not compressed, it has no difference with
366  * the RAW access mode. However, if the inode is compressed,
367  * users should pass a valid (m_lblk, m_lofs) pair, and get
368  * the needed m_pblk, m_pofs, m_len to get the compressed data
369  * and the updated m_lblk, m_lofs which indicates the start
370  * of the corresponding uncompressed data in the file.
371  */
372 enum {
373 	BH_Zipped = BH_PrivateStart,
374 	BH_FullMapped,
375 };
376 
377 /* Has a disk mapping */
378 #define EROFS_MAP_MAPPED	(1 << BH_Mapped)
379 /* Located in metadata (could be copied from bd_inode) */
380 #define EROFS_MAP_META		(1 << BH_Meta)
381 /* The extent has been compressed */
382 #define EROFS_MAP_ZIPPED	(1 << BH_Zipped)
383 /* The length of extent is full */
384 #define EROFS_MAP_FULL_MAPPED	(1 << BH_FullMapped)
385 
386 struct erofs_map_blocks {
387 	erofs_off_t m_pa, m_la;
388 	u64 m_plen, m_llen;
389 
390 	unsigned int m_flags;
391 
392 	struct page *mpage;
393 };
394 
395 /* Flags used by erofs_map_blocks() */
396 #define EROFS_GET_BLOCKS_RAW    0x0001
397 
398 /* zmap.c */
399 #ifdef CONFIG_EROFS_FS_ZIP
400 int z_erofs_fill_inode(struct inode *inode);
401 int z_erofs_map_blocks_iter(struct inode *inode,
402 			    struct erofs_map_blocks *map,
403 			    int flags);
404 #else
405 static inline int z_erofs_fill_inode(struct inode *inode) { return -EOPNOTSUPP; }
406 static inline int z_erofs_map_blocks_iter(struct inode *inode,
407 					  struct erofs_map_blocks *map,
408 					  int flags)
409 {
410 	return -EOPNOTSUPP;
411 }
412 #endif	/* !CONFIG_EROFS_FS_ZIP */
413 
414 /* data.c */
415 static inline struct bio *erofs_grab_bio(struct super_block *sb,
416 					 erofs_blk_t blkaddr,
417 					 unsigned int nr_pages,
418 					 void *bi_private, bio_end_io_t endio,
419 					 bool nofail)
420 {
421 	const gfp_t gfp = GFP_NOIO;
422 	struct bio *bio;
423 
424 	do {
425 		if (nr_pages == 1) {
426 			bio = bio_alloc(gfp | (nofail ? __GFP_NOFAIL : 0), 1);
427 			if (!bio) {
428 				DBG_BUGON(nofail);
429 				return ERR_PTR(-ENOMEM);
430 			}
431 			break;
432 		}
433 		bio = bio_alloc(gfp, nr_pages);
434 		nr_pages /= 2;
435 	} while (!bio);
436 
437 	bio->bi_end_io = endio;
438 	bio_set_dev(bio, sb->s_bdev);
439 	bio->bi_iter.bi_sector = (sector_t)blkaddr << LOG_SECTORS_PER_BLOCK;
440 	bio->bi_private = bi_private;
441 	return bio;
442 }
443 
444 static inline void __submit_bio(struct bio *bio, unsigned int op,
445 				unsigned int op_flags)
446 {
447 	bio_set_op_attrs(bio, op, op_flags);
448 	submit_bio(bio);
449 }
450 
451 struct page *__erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr,
452 				   bool prio, bool nofail);
453 
454 static inline struct page *erofs_get_meta_page(struct super_block *sb,
455 	erofs_blk_t blkaddr, bool prio)
456 {
457 	return __erofs_get_meta_page(sb, blkaddr, prio, false);
458 }
459 
460 int erofs_map_blocks(struct inode *, struct erofs_map_blocks *, int);
461 
462 static inline struct page *erofs_get_inline_page(struct inode *inode,
463 						 erofs_blk_t blkaddr)
464 {
465 	return erofs_get_meta_page(inode->i_sb, blkaddr,
466 				   S_ISDIR(inode->i_mode));
467 }
468 
469 /* inode.c */
470 static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
471 {
472 #if BITS_PER_LONG == 32
473 	return (nid >> 32) ^ (nid & 0xffffffff);
474 #else
475 	return nid;
476 #endif
477 }
478 
479 extern const struct inode_operations erofs_generic_iops;
480 extern const struct inode_operations erofs_symlink_iops;
481 extern const struct inode_operations erofs_fast_symlink_iops;
482 
483 static inline void set_inode_fast_symlink(struct inode *inode)
484 {
485 	inode->i_op = &erofs_fast_symlink_iops;
486 }
487 
488 static inline bool is_inode_fast_symlink(struct inode *inode)
489 {
490 	return inode->i_op == &erofs_fast_symlink_iops;
491 }
492 
493 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir);
494 int erofs_getattr(const struct path *path, struct kstat *stat,
495 		  u32 request_mask, unsigned int query_flags);
496 
497 /* namei.c */
498 extern const struct inode_operations erofs_dir_iops;
499 
500 int erofs_namei(struct inode *dir, struct qstr *name,
501 		erofs_nid_t *nid, unsigned int *d_type);
502 
503 /* dir.c */
504 extern const struct file_operations erofs_dir_fops;
505 
506 /* utils.c / zdata.c */
507 struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp, bool nofail);
508 
509 #if (EROFS_PCPUBUF_NR_PAGES > 0)
510 void *erofs_get_pcpubuf(unsigned int pagenr);
511 #define erofs_put_pcpubuf(buf) do { \
512 	(void)&(buf);	\
513 	preempt_enable();	\
514 } while (0)
515 #else
516 static inline void *erofs_get_pcpubuf(unsigned int pagenr)
517 {
518 	return ERR_PTR(-EOPNOTSUPP);
519 }
520 
521 #define erofs_put_pcpubuf(buf) do {} while (0)
522 #endif
523 
524 #ifdef CONFIG_EROFS_FS_ZIP
525 int erofs_workgroup_put(struct erofs_workgroup *grp);
526 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
527 					     pgoff_t index, bool *tag);
528 int erofs_register_workgroup(struct super_block *sb,
529 			     struct erofs_workgroup *grp, bool tag);
530 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
531 void erofs_shrinker_register(struct super_block *sb);
532 void erofs_shrinker_unregister(struct super_block *sb);
533 int __init erofs_init_shrinker(void);
534 void erofs_exit_shrinker(void);
535 int __init z_erofs_init_zip_subsystem(void);
536 void z_erofs_exit_zip_subsystem(void);
537 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
538 				       struct erofs_workgroup *egrp);
539 int erofs_try_to_free_cached_page(struct address_space *mapping,
540 				  struct page *page);
541 #else
542 static inline void erofs_shrinker_register(struct super_block *sb) {}
543 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
544 static inline int erofs_init_shrinker(void) { return 0; }
545 static inline void erofs_exit_shrinker(void) {}
546 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
547 static inline void z_erofs_exit_zip_subsystem(void) {}
548 #endif	/* !CONFIG_EROFS_FS_ZIP */
549 
550 #define EFSCORRUPTED    EUCLEAN         /* Filesystem is corrupted */
551 
552 #endif	/* __EROFS_INTERNAL_H */
553 
554