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