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