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