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