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