1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://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 __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 erofs_dbg(x, ...) pr_debug(x "\n", ##__VA_ARGS__)
35 #define DBG_BUGON BUG_ON
36 #else
37 #define erofs_dbg(x, ...) ((void)0)
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_fs_context {
50 #ifdef CONFIG_EROFS_FS_ZIP
51 /* current strategy of how to use managed cache */
52 unsigned char cache_strategy;
53 /* strategy of sync decompression (false - auto, true - force on) */
54 bool readahead_sync_decompress;
55
56 /* threshold for decompression synchronously */
57 unsigned int max_sync_decompress_pages;
58 #endif
59 unsigned int mount_opt;
60 };
61
62 /* all filesystem-wide lz4 configurations */
63 struct erofs_sb_lz4_info {
64 /* # of pages needed for EROFS lz4 rolling decompression */
65 u16 max_distance_pages;
66 /* maximum possible blocks for pclusters in the filesystem */
67 u16 max_pclusterblks;
68 };
69
70 struct erofs_sb_info {
71 #ifdef CONFIG_EROFS_FS_ZIP
72 /* list for all registered superblocks, mainly for shrinker */
73 struct list_head list;
74 struct mutex umount_mutex;
75
76 /* managed XArray arranged in physical block number */
77 struct xarray managed_pslots;
78
79 unsigned int shrinker_run_no;
80 u16 available_compr_algs;
81
82 /* pseudo inode to manage cached pages */
83 struct inode *managed_cache;
84
85 struct erofs_sb_lz4_info lz4;
86 #endif /* CONFIG_EROFS_FS_ZIP */
87 struct dax_device *dax_dev;
88 u32 blocks;
89 u32 meta_blkaddr;
90 #ifdef CONFIG_EROFS_FS_XATTR
91 u32 xattr_blkaddr;
92 #endif
93
94 /* inode slot unit size in bit shift */
95 unsigned char islotbits;
96
97 u32 sb_size; /* total superblock size */
98 u32 build_time_nsec;
99 u64 build_time;
100
101 /* what we really care is nid, rather than ino.. */
102 erofs_nid_t root_nid;
103 /* used for statfs, f_files - f_favail */
104 u64 inos;
105
106 u8 uuid[16]; /* 128-bit uuid for volume */
107 u8 volume_name[16]; /* volume name */
108 u32 feature_compat;
109 u32 feature_incompat;
110
111 struct erofs_fs_context ctx; /* options */
112 };
113
114 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
115 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
116
117 /* Mount flags set via mount options or defaults */
118 #define EROFS_MOUNT_XATTR_USER 0x00000010
119 #define EROFS_MOUNT_POSIX_ACL 0x00000020
120 #define EROFS_MOUNT_DAX_ALWAYS 0x00000040
121 #define EROFS_MOUNT_DAX_NEVER 0x00000080
122
123 #define clear_opt(ctx, option) ((ctx)->mount_opt &= ~EROFS_MOUNT_##option)
124 #define set_opt(ctx, option) ((ctx)->mount_opt |= EROFS_MOUNT_##option)
125 #define test_opt(ctx, option) ((ctx)->mount_opt & EROFS_MOUNT_##option)
126
127 enum {
128 EROFS_ZIP_CACHE_DISABLED,
129 EROFS_ZIP_CACHE_READAHEAD,
130 EROFS_ZIP_CACHE_READAROUND
131 };
132
133 #ifdef CONFIG_EROFS_FS_ZIP
134 #define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL)
135
136 /* basic unit of the workstation of a super_block */
137 struct erofs_workgroup {
138 /* the workgroup index in the workstation */
139 pgoff_t index;
140
141 /* overall workgroup reference count */
142 atomic_t refcount;
143 };
144
145 #if defined(CONFIG_SMP)
erofs_workgroup_try_to_freeze(struct erofs_workgroup * grp,int val)146 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
147 int val)
148 {
149 preempt_disable();
150 if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
151 preempt_enable();
152 return false;
153 }
154 return true;
155 }
156
erofs_workgroup_unfreeze(struct erofs_workgroup * grp,int orig_val)157 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
158 int orig_val)
159 {
160 /*
161 * other observers should notice all modifications
162 * in the freezing period.
163 */
164 smp_mb();
165 atomic_set(&grp->refcount, orig_val);
166 preempt_enable();
167 }
168
erofs_wait_on_workgroup_freezed(struct erofs_workgroup * grp)169 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
170 {
171 return atomic_cond_read_relaxed(&grp->refcount,
172 VAL != EROFS_LOCKED_MAGIC);
173 }
174 #else
erofs_workgroup_try_to_freeze(struct erofs_workgroup * grp,int val)175 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
176 int val)
177 {
178 preempt_disable();
179 /* no need to spin on UP platforms, let's just disable preemption. */
180 if (val != atomic_read(&grp->refcount)) {
181 preempt_enable();
182 return false;
183 }
184 return true;
185 }
186
erofs_workgroup_unfreeze(struct erofs_workgroup * grp,int orig_val)187 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
188 int orig_val)
189 {
190 preempt_enable();
191 }
192
erofs_wait_on_workgroup_freezed(struct erofs_workgroup * grp)193 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
194 {
195 int v = atomic_read(&grp->refcount);
196
197 /* workgroup is never freezed on uniprocessor systems */
198 DBG_BUGON(v == EROFS_LOCKED_MAGIC);
199 return v;
200 }
201 #endif /* !CONFIG_SMP */
202 #endif /* !CONFIG_EROFS_FS_ZIP */
203
204 /* we strictly follow PAGE_SIZE and no buffer head yet */
205 #define LOG_BLOCK_SIZE PAGE_SHIFT
206
207 #undef LOG_SECTORS_PER_BLOCK
208 #define LOG_SECTORS_PER_BLOCK (PAGE_SHIFT - 9)
209
210 #undef SECTORS_PER_BLOCK
211 #define SECTORS_PER_BLOCK (1 << SECTORS_PER_BLOCK)
212
213 #define EROFS_BLKSIZ (1 << LOG_BLOCK_SIZE)
214
215 #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
216 #error erofs cannot be used in this platform
217 #endif
218
219 #define ROOT_NID(sb) ((sb)->root_nid)
220
221 #define erofs_blknr(addr) ((addr) / EROFS_BLKSIZ)
222 #define erofs_blkoff(addr) ((addr) % EROFS_BLKSIZ)
223 #define blknr_to_addr(nr) ((erofs_off_t)(nr) * EROFS_BLKSIZ)
224
iloc(struct erofs_sb_info * sbi,erofs_nid_t nid)225 static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
226 {
227 return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
228 }
229
230 #define EROFS_FEATURE_FUNCS(name, compat, feature) \
231 static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
232 { \
233 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
234 }
235
236 EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING)
237 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
238 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
239 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
240
241 /* atomic flag definitions */
242 #define EROFS_I_EA_INITED_BIT 0
243 #define EROFS_I_Z_INITED_BIT 1
244
245 /* bitlock definitions (arranged in reverse order) */
246 #define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
247 #define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
248
249 struct erofs_inode {
250 erofs_nid_t nid;
251
252 /* atomic flags (including bitlocks) */
253 unsigned long flags;
254
255 unsigned char datalayout;
256 unsigned char inode_isize;
257 unsigned short xattr_isize;
258
259 unsigned int xattr_shared_count;
260 unsigned int *xattr_shared_xattrs;
261
262 union {
263 erofs_blk_t raw_blkaddr;
264 #ifdef CONFIG_EROFS_FS_ZIP
265 struct {
266 unsigned short z_advise;
267 unsigned char z_algorithmtype[2];
268 unsigned char z_logical_clusterbits;
269 };
270 #endif /* CONFIG_EROFS_FS_ZIP */
271 };
272 /* the corresponding vfs inode */
273 struct inode vfs_inode;
274 };
275
276 #define EROFS_I(ptr) \
277 container_of(ptr, struct erofs_inode, vfs_inode)
278
erofs_inode_datablocks(struct inode * inode)279 static inline unsigned long erofs_inode_datablocks(struct inode *inode)
280 {
281 /* since i_size cannot be changed */
282 return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
283 }
284
erofs_bitrange(unsigned int value,unsigned int bit,unsigned int bits)285 static inline unsigned int erofs_bitrange(unsigned int value, unsigned int bit,
286 unsigned int bits)
287 {
288
289 return (value >> bit) & ((1 << bits) - 1);
290 }
291
292
erofs_inode_version(unsigned int value)293 static inline unsigned int erofs_inode_version(unsigned int value)
294 {
295 return erofs_bitrange(value, EROFS_I_VERSION_BIT,
296 EROFS_I_VERSION_BITS);
297 }
298
erofs_inode_datalayout(unsigned int value)299 static inline unsigned int erofs_inode_datalayout(unsigned int value)
300 {
301 return erofs_bitrange(value, EROFS_I_DATALAYOUT_BIT,
302 EROFS_I_DATALAYOUT_BITS);
303 }
304
305 extern const struct super_operations erofs_sops;
306
307 extern const struct address_space_operations erofs_raw_access_aops;
308 extern const struct address_space_operations z_erofs_aops;
309
310 /*
311 * Logical to physical block mapping
312 *
313 * Different with other file systems, it is used for 2 access modes:
314 *
315 * 1) RAW access mode:
316 *
317 * Users pass a valid (m_lblk, m_lofs -- usually 0) pair,
318 * and get the valid m_pblk, m_pofs and the longest m_len(in bytes).
319 *
320 * Note that m_lblk in the RAW access mode refers to the number of
321 * the compressed ondisk block rather than the uncompressed
322 * in-memory block for the compressed file.
323 *
324 * m_pofs equals to m_lofs except for the inline data page.
325 *
326 * 2) Normal access mode:
327 *
328 * If the inode is not compressed, it has no difference with
329 * the RAW access mode. However, if the inode is compressed,
330 * users should pass a valid (m_lblk, m_lofs) pair, and get
331 * the needed m_pblk, m_pofs, m_len to get the compressed data
332 * and the updated m_lblk, m_lofs which indicates the start
333 * of the corresponding uncompressed data in the file.
334 */
335 enum {
336 BH_Zipped = BH_PrivateStart,
337 BH_FullMapped,
338 };
339
340 /* Has a disk mapping */
341 #define EROFS_MAP_MAPPED (1 << BH_Mapped)
342 /* Located in metadata (could be copied from bd_inode) */
343 #define EROFS_MAP_META (1 << BH_Meta)
344 /* The extent has been compressed */
345 #define EROFS_MAP_ZIPPED (1 << BH_Zipped)
346 /* The length of extent is full */
347 #define EROFS_MAP_FULL_MAPPED (1 << BH_FullMapped)
348
349 struct erofs_map_blocks {
350 erofs_off_t m_pa, m_la;
351 u64 m_plen, m_llen;
352
353 unsigned int m_flags;
354
355 struct page *mpage;
356 };
357
358 /* Flags used by erofs_map_blocks_flatmode() */
359 #define EROFS_GET_BLOCKS_RAW 0x0001
360
361 /* zmap.c */
362 #ifdef CONFIG_EROFS_FS_ZIP
363 int z_erofs_fill_inode(struct inode *inode);
364 int z_erofs_map_blocks_iter(struct inode *inode,
365 struct erofs_map_blocks *map,
366 int flags);
367 #else
z_erofs_fill_inode(struct inode * inode)368 static inline int z_erofs_fill_inode(struct inode *inode) { return -EOPNOTSUPP; }
z_erofs_map_blocks_iter(struct inode * inode,struct erofs_map_blocks * map,int flags)369 static inline int z_erofs_map_blocks_iter(struct inode *inode,
370 struct erofs_map_blocks *map,
371 int flags)
372 {
373 return -EOPNOTSUPP;
374 }
375 #endif /* !CONFIG_EROFS_FS_ZIP */
376
377 /* data.c */
378 extern const struct file_operations erofs_file_fops;
379 struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr);
380
381 /* inode.c */
erofs_inode_hash(erofs_nid_t nid)382 static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
383 {
384 #if BITS_PER_LONG == 32
385 return (nid >> 32) ^ (nid & 0xffffffff);
386 #else
387 return nid;
388 #endif
389 }
390
391 extern const struct inode_operations erofs_generic_iops;
392 extern const struct inode_operations erofs_symlink_iops;
393 extern const struct inode_operations erofs_fast_symlink_iops;
394
395 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir);
396 int erofs_getattr(const struct path *path, struct kstat *stat,
397 u32 request_mask, unsigned int query_flags);
398
399 /* namei.c */
400 extern const struct inode_operations erofs_dir_iops;
401
402 int erofs_namei(struct inode *dir, struct qstr *name,
403 erofs_nid_t *nid, unsigned int *d_type);
404
405 /* dir.c */
406 extern const struct file_operations erofs_dir_fops;
407
erofs_vm_map_ram(struct page ** pages,unsigned int count)408 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
409 {
410 int retried = 0;
411
412 while (1) {
413 void *p = vm_map_ram(pages, count, -1);
414
415 /* retry two more times (totally 3 times) */
416 if (p || ++retried >= 3)
417 return p;
418 vm_unmap_aliases();
419 }
420 return NULL;
421 }
422
423 /* pcpubuf.c */
424 void *erofs_get_pcpubuf(unsigned int requiredpages);
425 void erofs_put_pcpubuf(void *ptr);
426 int erofs_pcpubuf_growsize(unsigned int nrpages);
427 void erofs_pcpubuf_init(void);
428 void erofs_pcpubuf_exit(void);
429
430 /* utils.c / zdata.c */
431 struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp);
432
433 #ifdef CONFIG_EROFS_FS_ZIP
434 int erofs_workgroup_put(struct erofs_workgroup *grp);
435 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
436 pgoff_t index);
437 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
438 struct erofs_workgroup *grp);
439 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
440 void erofs_shrinker_register(struct super_block *sb);
441 void erofs_shrinker_unregister(struct super_block *sb);
442 int __init erofs_init_shrinker(void);
443 void erofs_exit_shrinker(void);
444 int __init z_erofs_init_zip_subsystem(void);
445 void z_erofs_exit_zip_subsystem(void);
446 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
447 struct erofs_workgroup *egrp);
448 int erofs_try_to_free_cached_page(struct address_space *mapping,
449 struct page *page);
450 int z_erofs_load_lz4_config(struct super_block *sb,
451 struct erofs_super_block *dsb,
452 struct z_erofs_lz4_cfgs *lz4, int len);
453 #else
erofs_shrinker_register(struct super_block * sb)454 static inline void erofs_shrinker_register(struct super_block *sb) {}
erofs_shrinker_unregister(struct super_block * sb)455 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
erofs_init_shrinker(void)456 static inline int erofs_init_shrinker(void) { return 0; }
erofs_exit_shrinker(void)457 static inline void erofs_exit_shrinker(void) {}
z_erofs_init_zip_subsystem(void)458 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
z_erofs_exit_zip_subsystem(void)459 static inline void z_erofs_exit_zip_subsystem(void) {}
z_erofs_load_lz4_config(struct super_block * sb,struct erofs_super_block * dsb,struct z_erofs_lz4_cfgs * lz4,int len)460 static inline int z_erofs_load_lz4_config(struct super_block *sb,
461 struct erofs_super_block *dsb,
462 struct z_erofs_lz4_cfgs *lz4, int len)
463 {
464 if (lz4 || dsb->u1.lz4_max_distance) {
465 erofs_err(sb, "lz4 algorithm isn't enabled");
466 return -EINVAL;
467 }
468 return 0;
469 }
470 #endif /* !CONFIG_EROFS_FS_ZIP */
471
472 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
473
474 #endif /* __EROFS_INTERNAL_H */
475
476