1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * linux/drivers/staging/erofs/internal.h
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
9 #ifndef __EROFS_INTERNAL_H
10 #define __EROFS_INTERNAL_H
13 #include <linux/dcache.h>
15 #include <linux/pagemap.h>
16 #include <linux/bio.h>
17 #include <linux/buffer_head.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
22 /* redefine pr_fmt "erofs: " */
24 #define pr_fmt(fmt) "erofs: " fmt
26 #define errln(x, ...) pr_err(x "\n", ##__VA_ARGS__)
27 #define infoln(x, ...) pr_info(x "\n", ##__VA_ARGS__)
28 #ifdef CONFIG_EROFS_FS_DEBUG
29 #define debugln(x, ...) pr_debug(x "\n", ##__VA_ARGS__)
30 #define DBG_BUGON BUG_ON
32 #define debugln(x, ...) ((void)0)
33 #define DBG_BUGON(x) ((void)(x))
34 #endif /* !CONFIG_EROFS_FS_DEBUG */
42 #ifdef CONFIG_EROFS_FAULT_INJECTION
43 extern const char *erofs_fault_name[FAULT_MAX];
44 #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
46 struct erofs_fault_info {
48 unsigned int inject_rate;
49 unsigned int inject_type;
51 #endif /* CONFIG_EROFS_FAULT_INJECTION */
53 /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
54 #define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
56 typedef u64 erofs_nid_t;
57 typedef u64 erofs_off_t;
58 /* data type for filesystem-wide blocks number */
59 typedef u32 erofs_blk_t;
61 struct erofs_sb_info {
62 #ifdef CONFIG_EROFS_FS_ZIP
63 /* list for all registered superblocks, mainly for shrinker */
64 struct list_head list;
65 struct mutex umount_mutex;
67 /* the dedicated workstation for compression */
68 struct radix_tree_root workstn_tree;
70 /* threshold for decompression synchronously */
71 unsigned int max_sync_decompress_pages;
73 unsigned int shrinker_run_no;
75 /* current strategy of how to use managed cache */
76 unsigned char cache_strategy;
78 /* pseudo inode to manage cached pages */
79 struct inode *managed_cache;
80 #endif /* CONFIG_EROFS_FS_ZIP */
83 #ifdef CONFIG_EROFS_FS_XATTR
87 /* inode slot unit size in bit shift */
88 unsigned char islotbits;
93 /* what we really care is nid, rather than ino.. */
95 /* used for statfs, f_files - f_favail */
98 u8 uuid[16]; /* 128-bit uuid for volume */
99 u8 volume_name[16]; /* volume name */
102 unsigned int mount_opt;
104 #ifdef CONFIG_EROFS_FAULT_INJECTION
105 struct erofs_fault_info fault_info; /* For fault injection */
109 #ifdef CONFIG_EROFS_FAULT_INJECTION
110 #define erofs_show_injection_info(type) \
111 infoln("inject %s in %s of %pS", erofs_fault_name[type], \
112 __func__, __builtin_return_address(0))
114 static inline bool time_to_inject(struct erofs_sb_info *sbi, int type)
116 struct erofs_fault_info *ffi = &sbi->fault_info;
118 if (!ffi->inject_rate)
121 if (!IS_FAULT_SET(ffi, type))
124 atomic_inc(&ffi->inject_ops);
125 if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
126 atomic_set(&ffi->inject_ops, 0);
132 static inline bool time_to_inject(struct erofs_sb_info *sbi, int type)
137 static inline void erofs_show_injection_info(int type)
140 #endif /* !CONFIG_EROFS_FAULT_INJECTION */
142 static inline void *erofs_kmalloc(struct erofs_sb_info *sbi,
143 size_t size, gfp_t flags)
145 if (time_to_inject(sbi, FAULT_KMALLOC)) {
146 erofs_show_injection_info(FAULT_KMALLOC);
149 return kmalloc(size, flags);
152 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
153 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
155 /* Mount flags set via mount options or defaults */
156 #define EROFS_MOUNT_XATTR_USER 0x00000010
157 #define EROFS_MOUNT_POSIX_ACL 0x00000020
158 #define EROFS_MOUNT_FAULT_INJECTION 0x00000040
160 #define clear_opt(sbi, option) ((sbi)->mount_opt &= ~EROFS_MOUNT_##option)
161 #define set_opt(sbi, option) ((sbi)->mount_opt |= EROFS_MOUNT_##option)
162 #define test_opt(sbi, option) ((sbi)->mount_opt & EROFS_MOUNT_##option)
164 #ifdef CONFIG_EROFS_FS_ZIP
166 EROFS_ZIP_CACHE_DISABLED,
167 EROFS_ZIP_CACHE_READAHEAD,
168 EROFS_ZIP_CACHE_READAROUND
171 #define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL)
173 /* basic unit of the workstation of a super_block */
174 struct erofs_workgroup {
175 /* the workgroup index in the workstation */
178 /* overall workgroup reference count */
182 #if defined(CONFIG_SMP)
183 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
187 if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
194 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
198 * other observers should notice all modifications
199 * in the freezing period.
202 atomic_set(&grp->refcount, orig_val);
206 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
208 return atomic_cond_read_relaxed(&grp->refcount,
209 VAL != EROFS_LOCKED_MAGIC);
212 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
216 /* no need to spin on UP platforms, let's just disable preemption. */
217 if (val != atomic_read(&grp->refcount)) {
224 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
230 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
232 int v = atomic_read(&grp->refcount);
234 /* workgroup is never freezed on uniprocessor systems */
235 DBG_BUGON(v == EROFS_LOCKED_MAGIC);
238 #endif /* !CONFIG_SMP */
240 /* hard limit of pages per compressed cluster */
241 #define Z_EROFS_CLUSTER_MAX_PAGES (CONFIG_EROFS_FS_CLUSTER_PAGE_LIMIT)
242 #define EROFS_PCPUBUF_NR_PAGES Z_EROFS_CLUSTER_MAX_PAGES
244 #define EROFS_PCPUBUF_NR_PAGES 0
245 #endif /* !CONFIG_EROFS_FS_ZIP */
247 /* we strictly follow PAGE_SIZE and no buffer head yet */
248 #define LOG_BLOCK_SIZE PAGE_SHIFT
250 #undef LOG_SECTORS_PER_BLOCK
251 #define LOG_SECTORS_PER_BLOCK (PAGE_SHIFT - 9)
253 #undef SECTORS_PER_BLOCK
254 #define SECTORS_PER_BLOCK (1 << SECTORS_PER_BLOCK)
256 #define EROFS_BLKSIZ (1 << LOG_BLOCK_SIZE)
258 #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
259 #error erofs cannot be used in this platform
262 #define EROFS_IO_MAX_RETRIES_NOFAIL 5
264 #define ROOT_NID(sb) ((sb)->root_nid)
266 #define erofs_blknr(addr) ((addr) / EROFS_BLKSIZ)
267 #define erofs_blkoff(addr) ((addr) % EROFS_BLKSIZ)
268 #define blknr_to_addr(nr) ((erofs_off_t)(nr) * EROFS_BLKSIZ)
270 static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
272 return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
275 /* atomic flag definitions */
276 #define EROFS_V_EA_INITED_BIT 0
277 #define EROFS_V_Z_INITED_BIT 1
279 /* bitlock definitions (arranged in reverse order) */
280 #define EROFS_V_BL_XATTR_BIT (BITS_PER_LONG - 1)
281 #define EROFS_V_BL_Z_BIT (BITS_PER_LONG - 2)
286 /* atomic flags (including bitlocks) */
289 unsigned char datamode;
290 unsigned char inode_isize;
291 unsigned short xattr_isize;
293 unsigned int xattr_shared_count;
294 unsigned int *xattr_shared_xattrs;
297 erofs_blk_t raw_blkaddr;
298 #ifdef CONFIG_EROFS_FS_ZIP
300 unsigned short z_advise;
301 unsigned char z_algorithmtype[2];
302 unsigned char z_logical_clusterbits;
303 unsigned char z_physical_clusterbits[2];
305 #endif /* CONFIG_EROFS_FS_ZIP */
307 /* the corresponding vfs inode */
308 struct inode vfs_inode;
311 #define EROFS_V(ptr) \
312 container_of(ptr, struct erofs_vnode, vfs_inode)
314 #define __inode_advise(x, bit, bits) \
315 (((x) >> (bit)) & ((1 << (bits)) - 1))
317 #define __inode_version(advise) \
318 __inode_advise(advise, EROFS_I_VERSION_BIT, \
319 EROFS_I_VERSION_BITS)
321 #define __inode_data_mapping(advise) \
322 __inode_advise(advise, EROFS_I_DATA_MAPPING_BIT,\
323 EROFS_I_DATA_MAPPING_BITS)
325 static inline unsigned long inode_datablocks(struct inode *inode)
327 /* since i_size cannot be changed */
328 return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
331 static inline bool is_inode_layout_compression(struct inode *inode)
333 return erofs_inode_is_data_compressed(EROFS_V(inode)->datamode);
336 static inline bool is_inode_flat_inline(struct inode *inode)
338 return EROFS_V(inode)->datamode == EROFS_INODE_FLAT_INLINE;
341 extern const struct super_operations erofs_sops;
343 extern const struct address_space_operations erofs_raw_access_aops;
344 #ifdef CONFIG_EROFS_FS_ZIP
345 extern const struct address_space_operations z_erofs_vle_normalaccess_aops;
349 * Logical to physical block mapping, used by erofs_map_blocks()
351 * Different with other file systems, it is used for 2 access modes:
353 * 1) RAW access mode:
355 * Users pass a valid (m_lblk, m_lofs -- usually 0) pair,
356 * and get the valid m_pblk, m_pofs and the longest m_len(in bytes).
358 * Note that m_lblk in the RAW access mode refers to the number of
359 * the compressed ondisk block rather than the uncompressed
360 * in-memory block for the compressed file.
362 * m_pofs equals to m_lofs except for the inline data page.
364 * 2) Normal access mode:
366 * If the inode is not compressed, it has no difference with
367 * the RAW access mode. However, if the inode is compressed,
368 * users should pass a valid (m_lblk, m_lofs) pair, and get
369 * the needed m_pblk, m_pofs, m_len to get the compressed data
370 * and the updated m_lblk, m_lofs which indicates the start
371 * of the corresponding uncompressed data in the file.
374 BH_Zipped = BH_PrivateStart,
378 /* Has a disk mapping */
379 #define EROFS_MAP_MAPPED (1 << BH_Mapped)
380 /* Located in metadata (could be copied from bd_inode) */
381 #define EROFS_MAP_META (1 << BH_Meta)
382 /* The extent has been compressed */
383 #define EROFS_MAP_ZIPPED (1 << BH_Zipped)
384 /* The length of extent is full */
385 #define EROFS_MAP_FULL_MAPPED (1 << BH_FullMapped)
387 struct erofs_map_blocks {
388 erofs_off_t m_pa, m_la;
391 unsigned int m_flags;
396 /* Flags used by erofs_map_blocks() */
397 #define EROFS_GET_BLOCKS_RAW 0x0001
400 #ifdef CONFIG_EROFS_FS_ZIP
401 int z_erofs_fill_inode(struct inode *inode);
402 int z_erofs_map_blocks_iter(struct inode *inode,
403 struct erofs_map_blocks *map,
406 static inline int z_erofs_fill_inode(struct inode *inode) { return -EOPNOTSUPP; }
407 static inline int z_erofs_map_blocks_iter(struct inode *inode,
408 struct erofs_map_blocks *map,
413 #endif /* !CONFIG_EROFS_FS_ZIP */
416 static inline struct bio *erofs_grab_bio(struct super_block *sb,
418 unsigned int nr_pages,
419 void *bi_private, bio_end_io_t endio,
422 const gfp_t gfp = GFP_NOIO;
427 bio = bio_alloc(gfp | (nofail ? __GFP_NOFAIL : 0), 1);
428 if (unlikely(!bio)) {
430 return ERR_PTR(-ENOMEM);
434 bio = bio_alloc(gfp, nr_pages);
436 } while (unlikely(!bio));
438 bio->bi_end_io = endio;
439 bio_set_dev(bio, sb->s_bdev);
440 bio->bi_iter.bi_sector = (sector_t)blkaddr << LOG_SECTORS_PER_BLOCK;
441 bio->bi_private = bi_private;
445 static inline void __submit_bio(struct bio *bio, unsigned int op,
446 unsigned int op_flags)
448 bio_set_op_attrs(bio, op, op_flags);
452 struct page *__erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr,
453 bool prio, bool nofail);
455 static inline struct page *erofs_get_meta_page(struct super_block *sb,
456 erofs_blk_t blkaddr, bool prio)
458 return __erofs_get_meta_page(sb, blkaddr, prio, false);
461 int erofs_map_blocks(struct inode *, struct erofs_map_blocks *, int);
463 static inline struct page *erofs_get_inline_page(struct inode *inode,
466 return erofs_get_meta_page(inode->i_sb, blkaddr,
467 S_ISDIR(inode->i_mode));
471 static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
473 #if BITS_PER_LONG == 32
474 return (nid >> 32) ^ (nid & 0xffffffff);
480 extern const struct inode_operations erofs_generic_iops;
481 extern const struct inode_operations erofs_symlink_iops;
482 extern const struct inode_operations erofs_fast_symlink_iops;
484 static inline void set_inode_fast_symlink(struct inode *inode)
486 inode->i_op = &erofs_fast_symlink_iops;
489 static inline bool is_inode_fast_symlink(struct inode *inode)
491 return inode->i_op == &erofs_fast_symlink_iops;
494 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir);
495 int erofs_getattr(const struct path *path, struct kstat *stat,
496 u32 request_mask, unsigned int query_flags);
499 extern const struct inode_operations erofs_dir_iops;
501 int erofs_namei(struct inode *dir, struct qstr *name,
502 erofs_nid_t *nid, unsigned int *d_type);
505 extern const struct file_operations erofs_dir_fops;
507 /* utils.c / zdata.c */
508 struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp, bool nofail);
510 #if (EROFS_PCPUBUF_NR_PAGES > 0)
511 void *erofs_get_pcpubuf(unsigned int pagenr);
512 #define erofs_put_pcpubuf(buf) do { \
517 static inline void *erofs_get_pcpubuf(unsigned int pagenr)
519 return ERR_PTR(-EOPNOTSUPP);
522 #define erofs_put_pcpubuf(buf) do {} while (0)
525 #ifdef CONFIG_EROFS_FS_ZIP
526 int erofs_workgroup_put(struct erofs_workgroup *grp);
527 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
528 pgoff_t index, bool *tag);
529 int erofs_register_workgroup(struct super_block *sb,
530 struct erofs_workgroup *grp, bool tag);
531 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
532 void erofs_shrinker_register(struct super_block *sb);
533 void erofs_shrinker_unregister(struct super_block *sb);
534 int __init erofs_init_shrinker(void);
535 void erofs_exit_shrinker(void);
536 int __init z_erofs_init_zip_subsystem(void);
537 void z_erofs_exit_zip_subsystem(void);
538 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
539 struct erofs_workgroup *egrp);
540 int erofs_try_to_free_cached_page(struct address_space *mapping,
543 static inline void erofs_shrinker_register(struct super_block *sb) {}
544 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
545 static inline int erofs_init_shrinker(void) { return 0; }
546 static inline void erofs_exit_shrinker(void) {}
547 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
548 static inline void z_erofs_exit_zip_subsystem(void) {}
549 #endif /* !CONFIG_EROFS_FS_ZIP */
551 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
553 #endif /* __EROFS_INTERNAL_H */