OSDN Git Service

staging: erofs: add the missing __init tags
[uclinux-h8/linux.git] / drivers / staging / erofs / internal.h
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * linux/drivers/staging/erofs/internal.h
4  *
5  * Copyright (C) 2017-2018 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Gao Xiang <gaoxiang25@huawei.com>
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file COPYING in the main directory of the Linux
11  * distribution for more details.
12  */
13 #ifndef __INTERNAL_H
14 #define __INTERNAL_H
15
16 #include <linux/fs.h>
17 #include <linux/dcache.h>
18 #include <linux/mm.h>
19 #include <linux/pagemap.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/cleancache.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include "erofs_fs.h"
26
27 /* redefine pr_fmt "erofs: " */
28 #undef pr_fmt
29 #define pr_fmt(fmt) "erofs: " fmt
30
31 #define errln(x, ...)   pr_err(x "\n", ##__VA_ARGS__)
32 #define infoln(x, ...)  pr_info(x "\n", ##__VA_ARGS__)
33 #ifdef CONFIG_EROFS_FS_DEBUG
34 #define debugln(x, ...) pr_debug(x "\n", ##__VA_ARGS__)
35
36 #define dbg_might_sleep         might_sleep
37 #define DBG_BUGON               BUG_ON
38 #else
39 #define debugln(x, ...)         ((void)0)
40
41 #define dbg_might_sleep()       ((void)0)
42 #define DBG_BUGON(...)          ((void)0)
43 #endif
44
45 enum {
46         FAULT_KMALLOC,
47         FAULT_MAX,
48 };
49
50 #ifdef CONFIG_EROFS_FAULT_INJECTION
51 extern char *erofs_fault_name[FAULT_MAX];
52 #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
53
54 struct erofs_fault_info {
55         atomic_t inject_ops;
56         unsigned int inject_rate;
57         unsigned int inject_type;
58 };
59 #endif
60
61 #ifdef CONFIG_EROFS_FS_ZIP_CACHE_BIPOLAR
62 #define EROFS_FS_ZIP_CACHE_LVL  (2)
63 #elif defined(EROFS_FS_ZIP_CACHE_UNIPOLAR)
64 #define EROFS_FS_ZIP_CACHE_LVL  (1)
65 #else
66 #define EROFS_FS_ZIP_CACHE_LVL  (0)
67 #endif
68
69 #if (!defined(EROFS_FS_HAS_MANAGED_CACHE) && (EROFS_FS_ZIP_CACHE_LVL > 0))
70 #define EROFS_FS_HAS_MANAGED_CACHE
71 #endif
72
73 /* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
74 #define EROFS_SUPER_MAGIC   EROFS_SUPER_MAGIC_V1
75
76 typedef u64 erofs_nid_t;
77
78 struct erofs_sb_info {
79         /* list for all registered superblocks, mainly for shrinker */
80         struct list_head list;
81         struct mutex umount_mutex;
82
83         u32 blocks;
84         u32 meta_blkaddr;
85 #ifdef CONFIG_EROFS_FS_XATTR
86         u32 xattr_blkaddr;
87 #endif
88
89         /* inode slot unit size in bit shift */
90         unsigned char islotbits;
91 #ifdef CONFIG_EROFS_FS_ZIP
92         /* cluster size in bit shift */
93         unsigned char clusterbits;
94
95         /* the dedicated workstation for compression */
96         struct radix_tree_root workstn_tree;
97
98         /* threshold for decompression synchronously */
99         unsigned int max_sync_decompress_pages;
100
101 #ifdef EROFS_FS_HAS_MANAGED_CACHE
102         struct inode *managed_cache;
103 #endif
104
105 #endif
106
107         u32 build_time_nsec;
108         u64 build_time;
109
110         /* what we really care is nid, rather than ino.. */
111         erofs_nid_t root_nid;
112         /* used for statfs, f_files - f_favail */
113         u64 inos;
114
115         u8 uuid[16];                    /* 128-bit uuid for volume */
116         u8 volume_name[16];             /* volume name */
117         char *dev_name;
118
119         unsigned int mount_opt;
120         unsigned int shrinker_run_no;
121
122 #ifdef CONFIG_EROFS_FAULT_INJECTION
123         struct erofs_fault_info fault_info;     /* For fault injection */
124 #endif
125 };
126
127 #ifdef CONFIG_EROFS_FAULT_INJECTION
128 #define erofs_show_injection_info(type)                                 \
129         infoln("inject %s in %s of %pS", erofs_fault_name[type],        \
130                 __func__, __builtin_return_address(0))
131
132 static inline bool time_to_inject(struct erofs_sb_info *sbi, int type)
133 {
134         struct erofs_fault_info *ffi = &sbi->fault_info;
135
136         if (!ffi->inject_rate)
137                 return false;
138
139         if (!IS_FAULT_SET(ffi, type))
140                 return false;
141
142         atomic_inc(&ffi->inject_ops);
143         if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
144                 atomic_set(&ffi->inject_ops, 0);
145                 return true;
146         }
147         return false;
148 }
149 #else
150 static inline bool time_to_inject(struct erofs_sb_info *sbi, int type)
151 {
152         return false;
153 }
154
155 static inline void erofs_show_injection_info(int type)
156 {
157 }
158 #endif
159
160 static inline void *erofs_kmalloc(struct erofs_sb_info *sbi,
161                                         size_t size, gfp_t flags)
162 {
163         if (time_to_inject(sbi, FAULT_KMALLOC)) {
164                 erofs_show_injection_info(FAULT_KMALLOC);
165                 return NULL;
166         }
167         return kmalloc(size, flags);
168 }
169
170 #define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
171 #define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
172
173 /* Mount flags set via mount options or defaults */
174 #define EROFS_MOUNT_XATTR_USER          0x00000010
175 #define EROFS_MOUNT_POSIX_ACL           0x00000020
176 #define EROFS_MOUNT_FAULT_INJECTION     0x00000040
177
178 #define clear_opt(sbi, option)  ((sbi)->mount_opt &= ~EROFS_MOUNT_##option)
179 #define set_opt(sbi, option)    ((sbi)->mount_opt |= EROFS_MOUNT_##option)
180 #define test_opt(sbi, option)   ((sbi)->mount_opt & EROFS_MOUNT_##option)
181
182 #ifdef CONFIG_EROFS_FS_ZIP
183 #define erofs_workstn_lock(sbi)         xa_lock(&(sbi)->workstn_tree)
184 #define erofs_workstn_unlock(sbi)       xa_unlock(&(sbi)->workstn_tree)
185
186 /* basic unit of the workstation of a super_block */
187 struct erofs_workgroup {
188         /* the workgroup index in the workstation */
189         pgoff_t index;
190
191         /* overall workgroup reference count */
192         atomic_t refcount;
193 };
194
195 #define EROFS_LOCKED_MAGIC     (INT_MIN | 0xE0F510CCL)
196
197 static inline bool erofs_workgroup_try_to_freeze(
198         struct erofs_workgroup *grp, int v)
199 {
200 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
201         if (v != atomic_cmpxchg(&grp->refcount,
202                 v, EROFS_LOCKED_MAGIC))
203                 return false;
204         preempt_disable();
205 #else
206         preempt_disable();
207         if (atomic_read(&grp->refcount) != v) {
208                 preempt_enable();
209                 return false;
210         }
211 #endif
212         return true;
213 }
214
215 static inline void erofs_workgroup_unfreeze(
216         struct erofs_workgroup *grp, int v)
217 {
218 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
219         atomic_set(&grp->refcount, v);
220 #endif
221         preempt_enable();
222 }
223
224 static inline bool erofs_workgroup_get(struct erofs_workgroup *grp, int *ocnt)
225 {
226         const int locked = (int)EROFS_LOCKED_MAGIC;
227         int o;
228
229 repeat:
230         o = atomic_read(&grp->refcount);
231
232         /* spin if it is temporarily locked at the reclaim path */
233         if (unlikely(o == locked)) {
234 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
235                 do
236                         cpu_relax();
237                 while (atomic_read(&grp->refcount) == locked);
238 #endif
239                 goto repeat;
240         }
241
242         if (unlikely(o <= 0))
243                 return -1;
244
245         if (unlikely(atomic_cmpxchg(&grp->refcount, o, o + 1) != o))
246                 goto repeat;
247
248         *ocnt = o;
249         return 0;
250 }
251
252 #define __erofs_workgroup_get(grp)      atomic_inc(&(grp)->refcount)
253
254 extern int erofs_workgroup_put(struct erofs_workgroup *grp);
255
256 extern struct erofs_workgroup *erofs_find_workgroup(
257         struct super_block *sb, pgoff_t index, bool *tag);
258
259 extern int erofs_register_workgroup(struct super_block *sb,
260         struct erofs_workgroup *grp, bool tag);
261
262 extern unsigned long erofs_shrink_workstation(struct erofs_sb_info *sbi,
263         unsigned long nr_shrink, bool cleanup);
264
265 static inline void erofs_workstation_cleanup_all(struct super_block *sb)
266 {
267         erofs_shrink_workstation(EROFS_SB(sb), ~0UL, true);
268 }
269
270 #ifdef EROFS_FS_HAS_MANAGED_CACHE
271 #define EROFS_UNALLOCATED_CACHED_PAGE   ((void *)0x5F0EF00D)
272
273 extern int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
274         struct erofs_workgroup *egrp);
275 extern int erofs_try_to_free_cached_page(struct address_space *mapping,
276         struct page *page);
277 #endif
278
279 #define DEFAULT_MAX_SYNC_DECOMPRESS_PAGES       3
280
281 static inline bool __should_decompress_synchronously(struct erofs_sb_info *sbi,
282                                                      unsigned int nr)
283 {
284         return nr <= sbi->max_sync_decompress_pages;
285 }
286
287 int __init z_erofs_init_zip_subsystem(void);
288 void z_erofs_exit_zip_subsystem(void);
289 #else
290 /* dummy initializer/finalizer for the decompression subsystem */
291 static inline int z_erofs_init_zip_subsystem(void) { return 0; }
292 static inline void z_erofs_exit_zip_subsystem(void) {}
293 #endif
294
295 /* we strictly follow PAGE_SIZE and no buffer head yet */
296 #define LOG_BLOCK_SIZE          PAGE_SHIFT
297
298 #undef LOG_SECTORS_PER_BLOCK
299 #define LOG_SECTORS_PER_BLOCK   (PAGE_SHIFT - 9)
300
301 #undef SECTORS_PER_BLOCK
302 #define SECTORS_PER_BLOCK       (1 << SECTORS_PER_BLOCK)
303
304 #define EROFS_BLKSIZ            (1 << LOG_BLOCK_SIZE)
305
306 #if (EROFS_BLKSIZ % 4096 || !EROFS_BLKSIZ)
307 #error erofs cannot be used in this platform
308 #endif
309
310 #define ROOT_NID(sb)            ((sb)->root_nid)
311
312 #ifdef CONFIG_EROFS_FS_ZIP
313 /* hard limit of pages per compressed cluster */
314 #define Z_EROFS_CLUSTER_MAX_PAGES       (CONFIG_EROFS_FS_CLUSTER_PAGE_LIMIT)
315
316 /* page count of a compressed cluster */
317 #define erofs_clusterpages(sbi)         ((1 << (sbi)->clusterbits) / PAGE_SIZE)
318 #endif
319
320 typedef u64 erofs_off_t;
321
322 /* data type for filesystem-wide blocks number */
323 typedef u32 erofs_blk_t;
324
325 #define erofs_blknr(addr)       ((addr) / EROFS_BLKSIZ)
326 #define erofs_blkoff(addr)      ((addr) % EROFS_BLKSIZ)
327 #define blknr_to_addr(nr)       ((erofs_off_t)(nr) * EROFS_BLKSIZ)
328
329 static inline erofs_off_t iloc(struct erofs_sb_info *sbi, erofs_nid_t nid)
330 {
331         return blknr_to_addr(sbi->meta_blkaddr) + (nid << sbi->islotbits);
332 }
333
334 #define inode_set_inited_xattr(inode)   (EROFS_V(inode)->flags |= 1)
335 #define inode_has_inited_xattr(inode)   (EROFS_V(inode)->flags & 1)
336
337 struct erofs_vnode {
338         erofs_nid_t nid;
339         unsigned int flags;
340
341         unsigned char data_mapping_mode;
342         /* inline size in bytes */
343         unsigned char inode_isize;
344         unsigned short xattr_isize;
345
346         unsigned xattr_shared_count;
347         unsigned *xattr_shared_xattrs;
348
349         erofs_blk_t raw_blkaddr;
350
351         /* the corresponding vfs inode */
352         struct inode vfs_inode;
353 };
354
355 #define EROFS_V(ptr)    \
356         container_of(ptr, struct erofs_vnode, vfs_inode)
357
358 #define __inode_advise(x, bit, bits) \
359         (((x) >> (bit)) & ((1 << (bits)) - 1))
360
361 #define __inode_version(advise) \
362         __inode_advise(advise, EROFS_I_VERSION_BIT,     \
363                 EROFS_I_VERSION_BITS)
364
365 #define __inode_data_mapping(advise)    \
366         __inode_advise(advise, EROFS_I_DATA_MAPPING_BIT,\
367                 EROFS_I_DATA_MAPPING_BITS)
368
369 static inline unsigned long inode_datablocks(struct inode *inode)
370 {
371         /* since i_size cannot be changed */
372         return DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
373 }
374
375 static inline bool is_inode_layout_plain(struct inode *inode)
376 {
377         return EROFS_V(inode)->data_mapping_mode == EROFS_INODE_LAYOUT_PLAIN;
378 }
379
380 static inline bool is_inode_layout_compression(struct inode *inode)
381 {
382         return EROFS_V(inode)->data_mapping_mode ==
383                                         EROFS_INODE_LAYOUT_COMPRESSION;
384 }
385
386 static inline bool is_inode_layout_inline(struct inode *inode)
387 {
388         return EROFS_V(inode)->data_mapping_mode == EROFS_INODE_LAYOUT_INLINE;
389 }
390
391 extern const struct super_operations erofs_sops;
392 extern const struct inode_operations erofs_dir_iops;
393 extern const struct file_operations erofs_dir_fops;
394
395 extern const struct address_space_operations erofs_raw_access_aops;
396 #ifdef CONFIG_EROFS_FS_ZIP
397 extern const struct address_space_operations z_erofs_vle_normalaccess_aops;
398 #endif
399
400 /*
401  * Logical to physical block mapping, used by erofs_map_blocks()
402  *
403  * Different with other file systems, it is used for 2 access modes:
404  *
405  * 1) RAW access mode:
406  *
407  * Users pass a valid (m_lblk, m_lofs -- usually 0) pair,
408  * and get the valid m_pblk, m_pofs and the longest m_len(in bytes).
409  *
410  * Note that m_lblk in the RAW access mode refers to the number of
411  * the compressed ondisk block rather than the uncompressed
412  * in-memory block for the compressed file.
413  *
414  * m_pofs equals to m_lofs except for the inline data page.
415  *
416  * 2) Normal access mode:
417  *
418  * If the inode is not compressed, it has no difference with
419  * the RAW access mode. However, if the inode is compressed,
420  * users should pass a valid (m_lblk, m_lofs) pair, and get
421  * the needed m_pblk, m_pofs, m_len to get the compressed data
422  * and the updated m_lblk, m_lofs which indicates the start
423  * of the corresponding uncompressed data in the file.
424  */
425 enum {
426         BH_Zipped = BH_PrivateStart,
427 };
428
429 /* Has a disk mapping */
430 #define EROFS_MAP_MAPPED        (1 << BH_Mapped)
431 /* Located in metadata (could be copied from bd_inode) */
432 #define EROFS_MAP_META          (1 << BH_Meta)
433 /* The extent has been compressed */
434 #define EROFS_MAP_ZIPPED        (1 << BH_Zipped)
435
436 struct erofs_map_blocks {
437         erofs_off_t m_pa, m_la;
438         u64 m_plen, m_llen;
439
440         unsigned int m_flags;
441 };
442
443 /* Flags used by erofs_map_blocks() */
444 #define EROFS_GET_BLOCKS_RAW    0x0001
445
446 /* data.c */
447 static inline struct bio *
448 erofs_grab_bio(struct super_block *sb,
449                erofs_blk_t blkaddr, unsigned int nr_pages,
450                bio_end_io_t endio, bool nofail)
451 {
452         const gfp_t gfp = GFP_NOIO;
453         struct bio *bio;
454
455         do {
456                 if (nr_pages == 1) {
457                         bio = bio_alloc(gfp | (nofail ? __GFP_NOFAIL : 0), 1);
458                         if (unlikely(bio == NULL)) {
459                                 DBG_BUGON(nofail);
460                                 return ERR_PTR(-ENOMEM);
461                         }
462                         break;
463                 }
464                 bio = bio_alloc(gfp, nr_pages);
465                 nr_pages /= 2;
466         } while (unlikely(bio == NULL));
467
468         bio->bi_end_io = endio;
469         bio_set_dev(bio, sb->s_bdev);
470         bio->bi_iter.bi_sector = (sector_t)blkaddr << LOG_SECTORS_PER_BLOCK;
471         return bio;
472 }
473
474 static inline void __submit_bio(struct bio *bio, unsigned op, unsigned op_flags)
475 {
476         bio_set_op_attrs(bio, op, op_flags);
477         submit_bio(bio);
478 }
479
480 #ifndef CONFIG_EROFS_FS_IO_MAX_RETRIES
481 #define EROFS_IO_MAX_RETRIES_NOFAIL     0
482 #else
483 #define EROFS_IO_MAX_RETRIES_NOFAIL     CONFIG_EROFS_FS_IO_MAX_RETRIES
484 #endif
485
486 extern struct page *__erofs_get_meta_page(struct super_block *sb,
487         erofs_blk_t blkaddr, bool prio, bool nofail);
488
489 static inline struct page *erofs_get_meta_page(struct super_block *sb,
490         erofs_blk_t blkaddr, bool prio)
491 {
492         return __erofs_get_meta_page(sb, blkaddr, prio, false);
493 }
494
495 static inline struct page *erofs_get_meta_page_nofail(struct super_block *sb,
496         erofs_blk_t blkaddr, bool prio)
497 {
498         return __erofs_get_meta_page(sb, blkaddr, prio, true);
499 }
500
501 extern int erofs_map_blocks(struct inode *, struct erofs_map_blocks *, int);
502 extern int erofs_map_blocks_iter(struct inode *, struct erofs_map_blocks *,
503         struct page **, int);
504
505 struct erofs_map_blocks_iter {
506         struct erofs_map_blocks map;
507         struct page *mpage;
508 };
509
510
511 static inline struct page *
512 erofs_get_inline_page(struct inode *inode,
513                       erofs_blk_t blkaddr)
514 {
515         return erofs_get_meta_page(inode->i_sb,
516                 blkaddr, S_ISDIR(inode->i_mode));
517 }
518
519 /* inode.c */
520 extern struct inode *erofs_iget(struct super_block *sb,
521         erofs_nid_t nid, bool dir);
522
523 /* dir.c */
524 int erofs_namei(struct inode *dir, struct qstr *name,
525         erofs_nid_t *nid, unsigned *d_type);
526
527 #ifdef CONFIG_EROFS_FS_XATTR
528 /* xattr.c */
529 extern const struct xattr_handler *erofs_xattr_handlers[];
530
531 /* symlink and special inode */
532 extern const struct inode_operations erofs_symlink_xattr_iops;
533 extern const struct inode_operations erofs_fast_symlink_xattr_iops;
534 extern const struct inode_operations erofs_special_inode_operations;
535 #endif
536
537 static inline void set_inode_fast_symlink(struct inode *inode)
538 {
539 #ifdef CONFIG_EROFS_FS_XATTR
540         inode->i_op = &erofs_fast_symlink_xattr_iops;
541 #else
542         inode->i_op = &simple_symlink_inode_operations;
543 #endif
544 }
545
546 static inline bool is_inode_fast_symlink(struct inode *inode)
547 {
548 #ifdef CONFIG_EROFS_FS_XATTR
549         return inode->i_op == &erofs_fast_symlink_xattr_iops;
550 #else
551         return inode->i_op == &simple_symlink_inode_operations;
552 #endif
553 }
554
555 static inline void *erofs_vmap(struct page **pages, unsigned int count)
556 {
557 #ifdef CONFIG_EROFS_FS_USE_VM_MAP_RAM
558         int i = 0;
559
560         while (1) {
561                 void *addr = vm_map_ram(pages, count, -1, PAGE_KERNEL);
562                 /* retry two more times (totally 3 times) */
563                 if (addr != NULL || ++i >= 3)
564                         return addr;
565                 vm_unmap_aliases();
566         }
567         return NULL;
568 #else
569         return vmap(pages, count, VM_MAP, PAGE_KERNEL);
570 #endif
571 }
572
573 static inline void erofs_vunmap(const void *mem, unsigned int count)
574 {
575 #ifdef CONFIG_EROFS_FS_USE_VM_MAP_RAM
576         vm_unmap_ram(mem, count);
577 #else
578         vunmap(mem);
579 #endif
580 }
581
582 /* utils.c */
583 extern struct page *erofs_allocpage(struct list_head *pool, gfp_t gfp);
584
585 extern void erofs_register_super(struct super_block *sb);
586 extern void erofs_unregister_super(struct super_block *sb);
587
588 extern unsigned long erofs_shrink_count(struct shrinker *shrink,
589         struct shrink_control *sc);
590 extern unsigned long erofs_shrink_scan(struct shrinker *shrink,
591         struct shrink_control *sc);
592
593 #ifndef lru_to_page
594 #define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
595 #endif
596
597 #endif
598