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/buffer_head.h>
16 #include <linux/magic.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/iomap.h>
20 #include "erofs_fs.h"
21
22 /* redefine pr_fmt "erofs: " */
23 #undef pr_fmt
24 #define pr_fmt(fmt) "erofs: " fmt
25
26 __printf(3, 4) void _erofs_err(struct super_block *sb,
27 const char *function, const char *fmt, ...);
28 #define erofs_err(sb, fmt, ...) \
29 _erofs_err(sb, __func__, fmt "\n", ##__VA_ARGS__)
30 __printf(3, 4) void _erofs_info(struct super_block *sb,
31 const char *function, const char *fmt, ...);
32 #define erofs_info(sb, fmt, ...) \
33 _erofs_info(sb, __func__, fmt "\n", ##__VA_ARGS__)
34 #ifdef CONFIG_EROFS_FS_DEBUG
35 #define erofs_dbg(x, ...) pr_debug(x "\n", ##__VA_ARGS__)
36 #define DBG_BUGON BUG_ON
37 #else
38 #define erofs_dbg(x, ...) ((void)0)
39 #define DBG_BUGON(x) ((void)(x))
40 #endif /* !CONFIG_EROFS_FS_DEBUG */
41
42 /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
43 #define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
44
45 typedef u64 erofs_nid_t;
46 typedef u64 erofs_off_t;
47 /* data type for filesystem-wide blocks number */
48 typedef u32 erofs_blk_t;
49
50 struct erofs_device_info {
51 char *path;
52 struct block_device *bdev;
53 struct dax_device *dax_dev;
54
55 u32 blocks;
56 u32 mapped_blkaddr;
57 };
58
59 struct erofs_mount_opts {
60 #ifdef CONFIG_EROFS_FS_ZIP
61 /* current strategy of how to use managed cache */
62 unsigned char cache_strategy;
63 /* strategy of sync decompression (false - auto, true - force on) */
64 bool readahead_sync_decompress;
65
66 /* threshold for decompression synchronously */
67 unsigned int max_sync_decompress_pages;
68 #endif
69 unsigned int mount_opt;
70 };
71
72 struct erofs_dev_context {
73 struct idr tree;
74 struct rw_semaphore rwsem;
75
76 unsigned int extra_devices;
77 };
78
79 struct erofs_fs_context {
80 struct erofs_mount_opts opt;
81 struct erofs_dev_context *devs;
82 };
83
84 /* all filesystem-wide lz4 configurations */
85 struct erofs_sb_lz4_info {
86 /* # of pages needed for EROFS lz4 rolling decompression */
87 u16 max_distance_pages;
88 /* maximum possible blocks for pclusters in the filesystem */
89 u16 max_pclusterblks;
90 };
91
92 struct erofs_sb_info {
93 struct erofs_mount_opts opt; /* options */
94 #ifdef CONFIG_EROFS_FS_ZIP
95 /* list for all registered superblocks, mainly for shrinker */
96 struct list_head list;
97 struct mutex umount_mutex;
98
99 /* managed XArray arranged in physical block number */
100 struct xarray managed_pslots;
101
102 unsigned int shrinker_run_no;
103 u16 available_compr_algs;
104
105 /* pseudo inode to manage cached pages */
106 struct inode *managed_cache;
107
108 struct erofs_sb_lz4_info lz4;
109 #endif /* CONFIG_EROFS_FS_ZIP */
110 struct erofs_dev_context *devs;
111 struct dax_device *dax_dev;
112 u64 total_blocks;
113 u32 primarydevice_blocks;
114
115 u32 meta_blkaddr;
116 #ifdef CONFIG_EROFS_FS_XATTR
117 u32 xattr_blkaddr;
118 #endif
119 u16 device_id_mask; /* valid bits of device id to be used */
120
121 /* inode slot unit size in bit shift */
122 unsigned char islotbits;
123
124 u32 sb_size; /* total superblock size */
125 u32 build_time_nsec;
126 u64 build_time;
127
128 /* what we really care is nid, rather than ino.. */
129 erofs_nid_t root_nid;
130 /* used for statfs, f_files - f_favail */
131 u64 inos;
132
133 u8 uuid[16]; /* 128-bit uuid for volume */
134 u8 volume_name[16]; /* volume name */
135 u32 feature_compat;
136 u32 feature_incompat;
137
138 /* sysfs support */
139 struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
140 struct completion s_kobj_unregister;
141 };
142
143 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
144 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
145
146 /* Mount flags set via mount options or defaults */
147 #define EROFS_MOUNT_XATTR_USER 0x00000010
148 #define EROFS_MOUNT_POSIX_ACL 0x00000020
149 #define EROFS_MOUNT_DAX_ALWAYS 0x00000040
150 #define EROFS_MOUNT_DAX_NEVER 0x00000080
151
152 #define clear_opt(opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)
153 #define set_opt(opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)
154 #define test_opt(opt, option) ((opt)->mount_opt & EROFS_MOUNT_##option)
155
156 enum {
157 EROFS_ZIP_CACHE_DISABLED,
158 EROFS_ZIP_CACHE_READAHEAD,
159 EROFS_ZIP_CACHE_READAROUND
160 };
161
162 #ifdef CONFIG_EROFS_FS_ZIP
163 #define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL)
164
165 /* basic unit of the workstation of a super_block */
166 struct erofs_workgroup {
167 /* the workgroup index in the workstation */
168 pgoff_t index;
169
170 /* overall workgroup reference count */
171 atomic_t refcount;
172 };
173
174 #if defined(CONFIG_SMP)
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 if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
180 preempt_enable();
181 return false;
182 }
183 return true;
184 }
185
erofs_workgroup_unfreeze(struct erofs_workgroup * grp,int orig_val)186 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
187 int orig_val)
188 {
189 /*
190 * other observers should notice all modifications
191 * in the freezing period.
192 */
193 smp_mb();
194 atomic_set(&grp->refcount, orig_val);
195 preempt_enable();
196 }
197
erofs_wait_on_workgroup_freezed(struct erofs_workgroup * grp)198 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
199 {
200 return atomic_cond_read_relaxed(&grp->refcount,
201 VAL != EROFS_LOCKED_MAGIC);
202 }
203 #else
erofs_workgroup_try_to_freeze(struct erofs_workgroup * grp,int val)204 static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
205 int val)
206 {
207 preempt_disable();
208 /* no need to spin on UP platforms, let's just disable preemption. */
209 if (val != atomic_read(&grp->refcount)) {
210 preempt_enable();
211 return false;
212 }
213 return true;
214 }
215
erofs_workgroup_unfreeze(struct erofs_workgroup * grp,int orig_val)216 static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
217 int orig_val)
218 {
219 preempt_enable();
220 }
221
erofs_wait_on_workgroup_freezed(struct erofs_workgroup * grp)222 static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
223 {
224 int v = atomic_read(&grp->refcount);
225
226 /* workgroup is never freezed on uniprocessor systems */
227 DBG_BUGON(v == EROFS_LOCKED_MAGIC);
228 return v;
229 }
230 #endif /* !CONFIG_SMP */
231 #endif /* !CONFIG_EROFS_FS_ZIP */
232
233 /* we strictly follow PAGE_SIZE and no buffer head yet */
234 #define LOG_BLOCK_SIZE PAGE_SHIFT
235
236 #undef LOG_SECTORS_PER_BLOCK
237 #define LOG_SECTORS_PER_BLOCK (PAGE_SHIFT - 9)
238
239 #undef SECTORS_PER_BLOCK
240 #define SECTORS_PER_BLOCK (1 << SECTORS_PER_BLOCK)
241
242 #define EROFS_BLKSIZ (1 << LOG_BLOCK_SIZE)
243
244 #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
245 #error erofs cannot be used in this platform
246 #endif
247
248 #define ROOT_NID(sb) ((sb)->root_nid)
249
250 #define erofs_blknr(addr) ((addr) / EROFS_BLKSIZ)
251 #define erofs_blkoff(addr) ((addr) % EROFS_BLKSIZ)
252 #define blknr_to_addr(nr) ((erofs_off_t)(nr) * EROFS_BLKSIZ)
253
iloc(struct erofs_sb_info * sbi,erofs_nid_t nid)254 static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
255 {
256 return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
257 }
258
259 #define EROFS_FEATURE_FUNCS(name, compat, feature) \
260 static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
261 { \
262 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
263 }
264
265 EROFS_FEATURE_FUNCS(lz4_0padding, incompat, INCOMPAT_LZ4_0PADDING)
266 EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
267 EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
268 EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
269 EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
270 EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
271 EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
272
273 /* atomic flag definitions */
274 #define EROFS_I_EA_INITED_BIT 0
275 #define EROFS_I_Z_INITED_BIT 1
276
277 /* bitlock definitions (arranged in reverse order) */
278 #define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
279 #define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
280
281 struct erofs_inode {
282 erofs_nid_t nid;
283
284 /* atomic flags (including bitlocks) */
285 unsigned long flags;
286
287 unsigned char datalayout;
288 unsigned char inode_isize;
289 unsigned int xattr_isize;
290
291 unsigned int xattr_shared_count;
292 unsigned int *xattr_shared_xattrs;
293
294 union {
295 erofs_blk_t raw_blkaddr;
296 struct {
297 unsigned short chunkformat;
298 unsigned char chunkbits;
299 };
300 #ifdef CONFIG_EROFS_FS_ZIP
301 struct {
302 unsigned short z_advise;
303 unsigned char z_algorithmtype[2];
304 unsigned char z_logical_clusterbits;
305 };
306 #endif /* CONFIG_EROFS_FS_ZIP */
307 };
308 /* the corresponding vfs inode */
309 struct inode vfs_inode;
310 };
311
312 #define EROFS_I(ptr) \
313 container_of(ptr, struct erofs_inode, vfs_inode)
314
erofs_inode_datablocks(struct inode * inode)315 static inline unsigned long erofs_inode_datablocks(struct inode *inode)
316 {
317 /* since i_size cannot be changed */
318 return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
319 }
320
erofs_bitrange(unsigned int value,unsigned int bit,unsigned int bits)321 static inline unsigned int erofs_bitrange(unsigned int value, unsigned int bit,
322 unsigned int bits)
323 {
324
325 return (value >> bit) & ((1 << bits) - 1);
326 }
327
328
erofs_inode_version(unsigned int value)329 static inline unsigned int erofs_inode_version(unsigned int value)
330 {
331 return erofs_bitrange(value, EROFS_I_VERSION_BIT,
332 EROFS_I_VERSION_BITS);
333 }
334
erofs_inode_datalayout(unsigned int value)335 static inline unsigned int erofs_inode_datalayout(unsigned int value)
336 {
337 return erofs_bitrange(value, EROFS_I_DATALAYOUT_BIT,
338 EROFS_I_DATALAYOUT_BITS);
339 }
340
341 extern const struct super_operations erofs_sops;
342
343 extern const struct address_space_operations erofs_raw_access_aops;
344 extern const struct address_space_operations z_erofs_aops;
345
346 /*
347 * Logical to physical block mapping
348 *
349 * Different with other file systems, it is used for 2 access modes:
350 *
351 * 1) RAW access mode:
352 *
353 * Users pass a valid (m_lblk, m_lofs -- usually 0) pair,
354 * and get the valid m_pblk, m_pofs and the longest m_len(in bytes).
355 *
356 * Note that m_lblk in the RAW access mode refers to the number of
357 * the compressed ondisk block rather than the uncompressed
358 * in-memory block for the compressed file.
359 *
360 * m_pofs equals to m_lofs except for the inline data page.
361 *
362 * 2) Normal access mode:
363 *
364 * If the inode is not compressed, it has no difference with
365 * the RAW access mode. However, if the inode is compressed,
366 * users should pass a valid (m_lblk, m_lofs) pair, and get
367 * the needed m_pblk, m_pofs, m_len to get the compressed data
368 * and the updated m_lblk, m_lofs which indicates the start
369 * of the corresponding uncompressed data in the file.
370 */
371 enum {
372 BH_Encoded = BH_PrivateStart,
373 BH_FullMapped,
374 };
375
376 /* Has a disk mapping */
377 #define EROFS_MAP_MAPPED (1 << BH_Mapped)
378 /* Located in metadata (could be copied from bd_inode) */
379 #define EROFS_MAP_META (1 << BH_Meta)
380 /* The extent is encoded */
381 #define EROFS_MAP_ENCODED (1 << BH_Encoded)
382 /* The length of extent is full */
383 #define EROFS_MAP_FULL_MAPPED (1 << BH_FullMapped)
384
385 struct erofs_map_blocks {
386 erofs_off_t m_pa, m_la;
387 u64 m_plen, m_llen;
388
389 unsigned short m_deviceid;
390 char m_algorithmformat;
391 unsigned int m_flags;
392
393 struct page *mpage;
394 };
395
396 /* Flags used by erofs_map_blocks_flatmode() */
397 #define EROFS_GET_BLOCKS_RAW 0x0001
398 /*
399 * Used to get the exact decompressed length, e.g. fiemap (consider lookback
400 * approach instead if possible since it's more metadata lightweight.)
401 */
402 #define EROFS_GET_BLOCKS_FIEMAP 0x0002
403 /* Used to map the whole extent if non-negligible data is requested for LZMA */
404 #define EROFS_GET_BLOCKS_READMORE 0x0004
405
406 enum {
407 Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
408 Z_EROFS_COMPRESSION_RUNTIME_MAX
409 };
410
411 /* zmap.c */
412 extern const struct iomap_ops z_erofs_iomap_report_ops;
413
414 #ifdef CONFIG_EROFS_FS_ZIP
415 int z_erofs_fill_inode(struct inode *inode);
416 int z_erofs_map_blocks_iter(struct inode *inode,
417 struct erofs_map_blocks *map,
418 int flags);
419 #else
z_erofs_fill_inode(struct inode * inode)420 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)421 static inline int z_erofs_map_blocks_iter(struct inode *inode,
422 struct erofs_map_blocks *map,
423 int flags)
424 {
425 return -EOPNOTSUPP;
426 }
427 #endif /* !CONFIG_EROFS_FS_ZIP */
428
429 struct erofs_map_dev {
430 struct block_device *m_bdev;
431 struct dax_device *m_daxdev;
432
433 erofs_off_t m_pa;
434 unsigned int m_deviceid;
435 };
436
437 /* data.c */
438 extern const struct file_operations erofs_file_fops;
439 struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr);
440 int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
441 int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
442 u64 start, u64 len);
443
444 /* inode.c */
erofs_inode_hash(erofs_nid_t nid)445 static inline unsigned long erofs_inode_hash(erofs_nid_t nid)
446 {
447 #if BITS_PER_LONG == 32
448 return (nid >> 32) ^ (nid & 0xffffffff);
449 #else
450 return nid;
451 #endif
452 }
453
454 extern const struct inode_operations erofs_generic_iops;
455 extern const struct inode_operations erofs_symlink_iops;
456 extern const struct inode_operations erofs_fast_symlink_iops;
457
458 struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid, bool dir);
459 int erofs_getattr(const struct path *path, struct kstat *stat,
460 u32 request_mask, unsigned int query_flags);
461
462 /* namei.c */
463 extern const struct inode_operations erofs_dir_iops;
464
465 int erofs_namei(struct inode *dir, struct qstr *name,
466 erofs_nid_t *nid, unsigned int *d_type);
467
468 /* dir.c */
469 extern const struct file_operations erofs_dir_fops;
470
erofs_vm_map_ram(struct page ** pages,unsigned int count)471 static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
472 {
473 int retried = 0;
474
475 while (1) {
476 void *p = vm_map_ram(pages, count, -1);
477
478 /* retry two more times (totally 3 times) */
479 if (p || ++retried >= 3)
480 return p;
481 vm_unmap_aliases();
482 }
483 return NULL;
484 }
485
486 /* pcpubuf.c */
487 void *erofs_get_pcpubuf(unsigned int requiredpages);
488 void erofs_put_pcpubuf(void *ptr);
489 int erofs_pcpubuf_growsize(unsigned int nrpages);
490 void erofs_pcpubuf_init(void);
491 void erofs_pcpubuf_exit(void);
492
493 /* sysfs.c */
494 int erofs_register_sysfs(struct super_block *sb);
495 void erofs_unregister_sysfs(struct super_block *sb);
496 int __init erofs_init_sysfs(void);
497 void erofs_exit_sysfs(void);
498
499 /* utils.c / zdata.c */
500 struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
erofs_pagepool_add(struct page ** pagepool,struct page * page)501 static inline void erofs_pagepool_add(struct page **pagepool,
502 struct page *page)
503 {
504 set_page_private(page, (unsigned long)*pagepool);
505 *pagepool = page;
506 }
507 void erofs_release_pages(struct page **pagepool);
508
509 #ifdef CONFIG_EROFS_FS_ZIP
510 int erofs_workgroup_put(struct erofs_workgroup *grp);
511 struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
512 pgoff_t index);
513 struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
514 struct erofs_workgroup *grp);
515 void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
516 void erofs_shrinker_register(struct super_block *sb);
517 void erofs_shrinker_unregister(struct super_block *sb);
518 int __init erofs_init_shrinker(void);
519 void erofs_exit_shrinker(void);
520 int __init z_erofs_init_zip_subsystem(void);
521 void z_erofs_exit_zip_subsystem(void);
522 int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
523 struct erofs_workgroup *egrp);
524 int erofs_try_to_free_cached_page(struct page *page);
525 int z_erofs_load_lz4_config(struct super_block *sb,
526 struct erofs_super_block *dsb,
527 struct z_erofs_lz4_cfgs *lz4, int len);
528 #else
erofs_shrinker_register(struct super_block * sb)529 static inline void erofs_shrinker_register(struct super_block *sb) {}
erofs_shrinker_unregister(struct super_block * sb)530 static inline void erofs_shrinker_unregister(struct super_block *sb) {}
erofs_init_shrinker(void)531 static inline int erofs_init_shrinker(void) { return 0; }
erofs_exit_shrinker(void)532 static inline void erofs_exit_shrinker(void) {}
z_erofs_init_zip_subsystem(void)533 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
z_erofs_exit_zip_subsystem(void)534 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)535 static inline int z_erofs_load_lz4_config(struct super_block *sb,
536 struct erofs_super_block *dsb,
537 struct z_erofs_lz4_cfgs *lz4, int len)
538 {
539 if (lz4 || dsb->u1.lz4_max_distance) {
540 erofs_err(sb, "lz4 algorithm isn't enabled");
541 return -EINVAL;
542 }
543 return 0;
544 }
545 #endif /* !CONFIG_EROFS_FS_ZIP */
546
547 #ifdef CONFIG_EROFS_FS_ZIP_LZMA
548 int z_erofs_lzma_init(void);
549 void z_erofs_lzma_exit(void);
550 int z_erofs_load_lzma_config(struct super_block *sb,
551 struct erofs_super_block *dsb,
552 struct z_erofs_lzma_cfgs *lzma, int size);
553 #else
z_erofs_lzma_init(void)554 static inline int z_erofs_lzma_init(void) { return 0; }
z_erofs_lzma_exit(void)555 static inline int z_erofs_lzma_exit(void) { return 0; }
z_erofs_load_lzma_config(struct super_block * sb,struct erofs_super_block * dsb,struct z_erofs_lzma_cfgs * lzma,int size)556 static inline int z_erofs_load_lzma_config(struct super_block *sb,
557 struct erofs_super_block *dsb,
558 struct z_erofs_lzma_cfgs *lzma, int size) {
559 if (lzma) {
560 erofs_err(sb, "lzma algorithm isn't enabled");
561 return -EINVAL;
562 }
563 return 0;
564 }
565 #endif /* !CONFIG_EROFS_FS_ZIP */
566
567 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
568
569 #endif /* __EROFS_INTERNAL_H */
570