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