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/cleancache.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
23 /* redefine pr_fmt "erofs: " */
25 #define pr_fmt(fmt) "erofs: " fmt
27 #define errln(x, ...) pr_err(x "\n", ##__VA_ARGS__)
28 #define infoln(x, ...) pr_info(x "\n", ##__VA_ARGS__)
29 #ifdef CONFIG_EROFS_FS_DEBUG
30 #define debugln(x, ...) pr_debug(x "\n", ##__VA_ARGS__)
31 #define DBG_BUGON BUG_ON
33 #define debugln(x, ...) ((void)0)
34 #define DBG_BUGON(x) ((void)(x))
35 #endif /* !CONFIG_EROFS_FS_DEBUG */
43 #ifdef CONFIG_EROFS_FAULT_INJECTION
44 extern const char *erofs_fault_name[FAULT_MAX];
45 #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
47 struct erofs_fault_info {
49 unsigned int inject_rate;
50 unsigned int inject_type;
52 #endif /* CONFIG_EROFS_FAULT_INJECTION */
54 #ifdef CONFIG_EROFS_FS_ZIP_CACHE_BIPOLAR
55 #define EROFS_FS_ZIP_CACHE_LVL (2)
56 #elif defined(EROFS_FS_ZIP_CACHE_UNIPOLAR)
57 #define EROFS_FS_ZIP_CACHE_LVL (1)
59 #define EROFS_FS_ZIP_CACHE_LVL (0)
62 #if (!defined(EROFS_FS_HAS_MANAGED_CACHE) && (EROFS_FS_ZIP_CACHE_LVL > 0))
63 #define EROFS_FS_HAS_MANAGED_CACHE
66 /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
67 #define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
69 typedef u64 erofs_nid_t;
70 typedef u64 erofs_off_t;
71 /* data type for filesystem-wide blocks number */
72 typedef u32 erofs_blk_t;
74 struct erofs_sb_info {
75 #ifdef CONFIG_EROFS_FS_ZIP
76 /* list for all registered superblocks, mainly for shrinker */
77 struct list_head list;
78 struct mutex umount_mutex;
80 /* the dedicated workstation for compression */
81 struct radix_tree_root workstn_tree;
83 /* threshold for decompression synchronously */
84 unsigned int max_sync_decompress_pages;
86 unsigned int shrinker_run_no;
88 #ifdef EROFS_FS_HAS_MANAGED_CACHE
89 struct inode *managed_cache;
92 #endif /* CONFIG_EROFS_FS_ZIP */
95 #ifdef CONFIG_EROFS_FS_XATTR
99 /* inode slot unit size in bit shift */
100 unsigned char islotbits;
105 /* what we really care is nid, rather than ino.. */
106 erofs_nid_t root_nid;
107 /* used for statfs, f_files - f_favail */
110 u8 uuid[16]; /* 128-bit uuid for volume */
111 u8 volume_name[16]; /* volume name */
114 unsigned int mount_opt;
116 #ifdef CONFIG_EROFS_FAULT_INJECTION
117 struct erofs_fault_info fault_info; /* For fault injection */
121 #ifdef CONFIG_EROFS_FAULT_INJECTION
122 #define erofs_show_injection_info(type) \
123 infoln("inject %s in %s of %pS", erofs_fault_name[type], \
124 __func__, __builtin_return_address(0))
126 static inline bool time_to_inject(struct erofs_sb_info *sbi, int type)
128 struct erofs_fault_info *ffi = &sbi->fault_info;
130 if (!ffi->inject_rate)
133 if (!IS_FAULT_SET(ffi, type))
136 atomic_inc(&ffi->inject_ops);
137 if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
138 atomic_set(&ffi->inject_ops, 0);
144 static inline bool time_to_inject(struct erofs_sb_info *sbi, int type)
149 static inline void erofs_show_injection_info(int type)
152 #endif /* !CONFIG_EROFS_FAULT_INJECTION */
154 static inline void *erofs_kmalloc(struct erofs_sb_info *sbi,
155 size_t size, gfp_t flags)
157 if (time_to_inject(sbi, FAULT_KMALLOC)) {
158 erofs_show_injection_info(FAULT_KMALLOC);
161 return kmalloc(size, flags);
164 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
165 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
167 /* Mount flags set via mount options or defaults */
168 #define EROFS_MOUNT_XATTR_USER 0x00000010
169 #define EROFS_MOUNT_POSIX_ACL 0x00000020
170 #define EROFS_MOUNT_FAULT_INJECTION 0x00000040
172 #define clear_opt(sbi, option) ((sbi)->mount_opt &= ~EROFS_MOUNT_##option)
173 #define set_opt(sbi, option) ((sbi)->mount_opt |= EROFS_MOUNT_##option)
174 #define test_opt(sbi, option) ((sbi)->mount_opt & EROFS_MOUNT_##option)
176 #ifdef CONFIG_EROFS_FS_ZIP
177 #define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL)
179 /* basic unit of the workstation of a super_block */
180 struct erofs_workgroup {
181 /* the workgroup index in the workstation */
184 /* overall workgroup reference count */
188 #if defined(CONFIG_SMP)
189 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
193 if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
200 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
204 * other observers should notice all modifications
205 * in the freezing period.
208 atomic_set(&grp->refcount, orig_val);
212 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
214 return atomic_cond_read_relaxed(&grp->refcount,
215 VAL != EROFS_LOCKED_MAGIC);
218 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
222 /* no need to spin on UP platforms, let's just disable preemption. */
223 if (val != atomic_read(&grp->refcount)) {
230 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
236 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
238 int v = atomic_read(&grp->refcount);
240 /* workgroup is never freezed on uniprocessor systems */
241 DBG_BUGON(v == EROFS_LOCKED_MAGIC);
244 #endif /* !CONFIG_SMP */
246 /* hard limit of pages per compressed cluster */
247 #define Z_EROFS_CLUSTER_MAX_PAGES (CONFIG_EROFS_FS_CLUSTER_PAGE_LIMIT)
248 #define EROFS_PCPUBUF_NR_PAGES Z_EROFS_CLUSTER_MAX_PAGES
250 #define EROFS_PCPUBUF_NR_PAGES 0
251 #endif /* !CONFIG_EROFS_FS_ZIP */
253 /* we strictly follow PAGE_SIZE and no buffer head yet */
254 #define LOG_BLOCK_SIZE PAGE_SHIFT
256 #undef LOG_SECTORS_PER_BLOCK
257 #define LOG_SECTORS_PER_BLOCK (PAGE_SHIFT - 9)
259 #undef SECTORS_PER_BLOCK
260 #define SECTORS_PER_BLOCK (1 << SECTORS_PER_BLOCK)
262 #define EROFS_BLKSIZ (1 << LOG_BLOCK_SIZE)
264 #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
265 #error erofs cannot be used in this platform
268 #define EROFS_IO_MAX_RETRIES_NOFAIL 5
270 #define ROOT_NID(sb) ((sb)->root_nid)
272 #define erofs_blknr(addr) ((addr) / EROFS_BLKSIZ)
273 #define erofs_blkoff(addr) ((addr) % EROFS_BLKSIZ)
274 #define blknr_to_addr(nr) ((erofs_off_t)(nr) * EROFS_BLKSIZ)
276 static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
278 return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
281 /* atomic flag definitions */
282 #define EROFS_V_EA_INITED_BIT 0
283 #define EROFS_V_Z_INITED_BIT 1
285 /* bitlock definitions (arranged in reverse order) */
286 #define EROFS_V_BL_XATTR_BIT (BITS_PER_LONG - 1)
287 #define EROFS_V_BL_Z_BIT (BITS_PER_LONG - 2)
292 /* atomic flags (including bitlocks) */
295 unsigned char datamode;
296 unsigned char inode_isize;
297 unsigned short xattr_isize;
299 unsigned int xattr_shared_count;
300 unsigned int *xattr_shared_xattrs;
303 erofs_blk_t raw_blkaddr;
304 #ifdef CONFIG_EROFS_FS_ZIP
306 unsigned short z_advise;
307 unsigned char z_algorithmtype[2];
308 unsigned char z_logical_clusterbits;
309 unsigned char z_physical_clusterbits[2];
311 #endif /* CONFIG_EROFS_FS_ZIP */
313 /* the corresponding vfs inode */
314 struct inode vfs_inode;
317 #define EROFS_V(ptr) \
318 container_of(ptr, struct erofs_vnode, vfs_inode)
320 #define __inode_advise(x, bit, bits) \
321 (((x) >> (bit)) & ((1 << (bits)) - 1))
323 #define __inode_version(advise) \
324 __inode_advise(advise, EROFS_I_VERSION_BIT, \
325 EROFS_I_VERSION_BITS)
327 #define __inode_data_mapping(advise) \
328 __inode_advise(advise, EROFS_I_DATA_MAPPING_BIT,\
329 EROFS_I_DATA_MAPPING_BITS)
331 static inline unsigned long inode_datablocks(struct inode *inode)
333 /* since i_size cannot be changed */
334 return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
337 static inline bool is_inode_layout_compression(struct inode *inode)
339 return erofs_inode_is_data_compressed(EROFS_V(inode)->datamode);
342 static inline bool is_inode_flat_inline(struct inode *inode)
344 return EROFS_V(inode)->datamode == EROFS_INODE_FLAT_INLINE;
347 extern const struct super_operations erofs_sops;
349 extern const struct address_space_operations erofs_raw_access_aops;
350 #ifdef CONFIG_EROFS_FS_ZIP
351 extern const struct address_space_operations z_erofs_vle_normalaccess_aops;
355 * Logical to physical block mapping, used by erofs_map_blocks()
357 * Different with other file systems, it is used for 2 access modes:
359 * 1) RAW access mode:
361 * Users pass a valid (m_lblk, m_lofs -- usually 0) pair,
362 * and get the valid m_pblk, m_pofs and the longest m_len(in bytes).
364 * Note that m_lblk in the RAW access mode refers to the number of
365 * the compressed ondisk block rather than the uncompressed
366 * in-memory block for the compressed file.
368 * m_pofs equals to m_lofs except for the inline data page.
370 * 2) Normal access mode:
372 * If the inode is not compressed, it has no difference with
373 * the RAW access mode. However, if the inode is compressed,
374 * users should pass a valid (m_lblk, m_lofs) pair, and get
375 * the needed m_pblk, m_pofs, m_len to get the compressed data
376 * and the updated m_lblk, m_lofs which indicates the start
377 * of the corresponding uncompressed data in the file.
380 BH_Zipped = BH_PrivateStart,
384 /* Has a disk mapping */
385 #define EROFS_MAP_MAPPED (1 << BH_Mapped)
386 /* Located in metadata (could be copied from bd_inode) */
387 #define EROFS_MAP_META (1 << BH_Meta)
388 /* The extent has been compressed */
389 #define EROFS_MAP_ZIPPED (1 << BH_Zipped)
390 /* The length of extent is full */
391 #define EROFS_MAP_FULL_MAPPED (1 << BH_FullMapped)
393 struct erofs_map_blocks {
394 erofs_off_t m_pa, m_la;
397 unsigned int m_flags;
402 /* Flags used by erofs_map_blocks() */
403 #define EROFS_GET_BLOCKS_RAW 0x0001
406 #ifdef CONFIG_EROFS_FS_ZIP
407 int z_erofs_fill_inode(struct inode *inode);
408 int z_erofs_map_blocks_iter(struct inode *inode,
409 struct erofs_map_blocks *map,
412 static inline int z_erofs_fill_inode(struct inode *inode) { return -ENOTSUPP; }
413 static inline int z_erofs_map_blocks_iter(struct inode *inode,
414 struct erofs_map_blocks *map,
419 #endif /* !CONFIG_EROFS_FS_ZIP */
422 static inline struct bio *erofs_grab_bio(struct super_block *sb,
424 unsigned int nr_pages,
425 void *bi_private, bio_end_io_t endio,
428 const gfp_t gfp = GFP_NOIO;
433 bio = bio_alloc(gfp | (nofail ? __GFP_NOFAIL : 0), 1);
434 if (unlikely(!bio)) {
436 return ERR_PTR(-ENOMEM);
440 bio = bio_alloc(gfp, nr_pages);
442 } while (unlikely(!bio));
444 bio->bi_end_io = endio;
445 bio_set_dev(bio, sb->s_bdev);
446 bio->bi_iter.bi_sector = (sector_t)blkaddr << LOG_SECTORS_PER_BLOCK;
447 bio->bi_private = bi_private;
451 static inline void __submit_bio(struct bio *bio, unsigned int op,
452 unsigned int op_flags)
454 bio_set_op_attrs(bio, op, op_flags);
458 struct page *__erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr,
459 bool prio, bool nofail);
461 static inline struct page *erofs_get_meta_page(struct super_block *sb,
462 erofs_blk_t blkaddr, bool prio)
464 return __erofs_get_meta_page(sb, blkaddr, prio, false);
467 int erofs_map_blocks(struct inode *, struct erofs_map_blocks *, int);
469 static inline struct page *erofs_get_inline_page(struct inode *inode,
472 return erofs_get_meta_page(inode->i_sb, blkaddr,
473 S_ISDIR(inode->i_mode));
477 static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
479 #if BITS_PER_LONG == 32
480 return (nid >> 32) ^ (nid & 0xffffffff);
486 extern const struct inode_operations erofs_generic_iops;
487 extern const struct inode_operations erofs_symlink_iops;
488 extern const struct inode_operations erofs_fast_symlink_iops;
490 static inline void set_inode_fast_symlink(struct inode *inode)
492 inode->i_op = &erofs_fast_symlink_iops;
495 static inline bool is_inode_fast_symlink(struct inode *inode)
497 return inode->i_op == &erofs_fast_symlink_iops;
500 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir);
501 int erofs_getattr(const struct path *path, struct kstat *stat,
502 u32 request_mask, unsigned int query_flags);
505 extern const struct inode_operations erofs_dir_iops;
507 int erofs_namei(struct inode *dir, struct qstr *name,
508 erofs_nid_t *nid, unsigned int *d_type);
511 extern const struct file_operations erofs_dir_fops;
513 /* utils.c / zdata.c */
514 struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp, bool nofail);
516 #if (EROFS_PCPUBUF_NR_PAGES > 0)
517 void *erofs_get_pcpubuf(unsigned int pagenr);
518 #define erofs_put_pcpubuf(buf) do { \
523 static inline void *erofs_get_pcpubuf(unsigned int pagenr)
525 return ERR_PTR(-ENOTSUPP);
528 #define erofs_put_pcpubuf(buf) do {} while (0)
531 #ifdef CONFIG_EROFS_FS_ZIP
532 int erofs_workgroup_put(struct erofs_workgroup *grp);
533 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
534 pgoff_t index, bool *tag);
535 int erofs_register_workgroup(struct super_block *sb,
536 struct erofs_workgroup *grp, bool tag);
537 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
538 void erofs_shrinker_register(struct super_block *sb);
539 void erofs_shrinker_unregister(struct super_block *sb);
540 int __init erofs_init_shrinker(void);
541 void erofs_exit_shrinker(void);
542 int __init z_erofs_init_zip_subsystem(void);
543 void z_erofs_exit_zip_subsystem(void);
544 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
545 struct erofs_workgroup *egrp);
546 int erofs_try_to_free_cached_page(struct address_space *mapping,
549 static inline void erofs_shrinker_register(struct super_block *sb) {}
550 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
551 static inline int erofs_init_shrinker(void) { return 0; }
552 static inline void erofs_exit_shrinker(void) {}
553 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
554 static inline void z_erofs_exit_zip_subsystem(void) {}
555 #endif /* !CONFIG_EROFS_FS_ZIP */
557 #endif /* __EROFS_INTERNAL_H */