blob: 1e39c03357d1ec0ecf039f7e28ccb432e24618d2 [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2/*
Gao Xiangbfb86742018-07-26 20:21:45 +08003 * Copyright (C) 2017-2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiangc5aa9032021-08-20 18:00:19 +08005 * Copyright (C) 2021, Alibaba Cloud
Gao Xiangbfb86742018-07-26 20:21:45 +08006 */
Gao Xiang14f362b2019-07-31 23:57:36 +08007#ifndef __EROFS_INTERNAL_H
8#define __EROFS_INTERNAL_H
Gao Xiangbfb86742018-07-26 20:21:45 +08009
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>
Gao Xiang47e49372019-08-23 05:36:59 +080015#include <linux/magic.h>
Gao Xiangbfb86742018-07-26 20:21:45 +080016#include <linux/slab.h>
17#include <linux/vmalloc.h>
Gao Xiangeadcd6b2021-08-13 13:29:31 +080018#include <linux/iomap.h>
Gao Xiangbfb86742018-07-26 20:21:45 +080019#include "erofs_fs.h"
20
21/* redefine pr_fmt "erofs: " */
22#undef pr_fmt
23#define pr_fmt(fmt) "erofs: " fmt
24
Gao Xiang4f761fa2019-09-04 10:09:09 +080025__printf(3, 4) void _erofs_err(struct super_block *sb,
26 const char *function, const char *fmt, ...);
27#define erofs_err(sb, fmt, ...) \
28 _erofs_err(sb, __func__, fmt "\n", ##__VA_ARGS__)
29__printf(3, 4) void _erofs_info(struct super_block *sb,
30 const char *function, const char *fmt, ...);
31#define erofs_info(sb, fmt, ...) \
32 _erofs_info(sb, __func__, fmt "\n", ##__VA_ARGS__)
Gao Xiangbfb86742018-07-26 20:21:45 +080033#ifdef CONFIG_EROFS_FS_DEBUG
Gao Xiangbfb86742018-07-26 20:21:45 +080034#define DBG_BUGON BUG_ON
35#else
Gao Xiangeef16872018-11-23 01:15:59 +080036#define DBG_BUGON(x) ((void)(x))
Gao Xiang14f362b2019-07-31 23:57:36 +080037#endif /* !CONFIG_EROFS_FS_DEBUG */
Gao Xiangbfb86742018-07-26 20:21:45 +080038
39/* EROFS_SUPER_MAGIC_V1 to represent the whole file system */
40#define EROFS_SUPER_MAGIC EROFS_SUPER_MAGIC_V1
41
42typedef u64 erofs_nid_t;
Gao Xiang14f362b2019-07-31 23:57:36 +080043typedef u64 erofs_off_t;
44/* data type for filesystem-wide blocks number */
45typedef u32 erofs_blk_t;
Gao Xiangbfb86742018-07-26 20:21:45 +080046
Gao Xiangdfeab2e2021-10-14 16:10:10 +080047struct erofs_device_info {
48 char *path;
Jeffle Xu955b4782022-04-25 20:21:38 +080049 struct erofs_fscache *fscache;
Gao Xiangdfeab2e2021-10-14 16:10:10 +080050 struct block_device *bdev;
51 struct dax_device *dax_dev;
Christoph Hellwigcd913c72021-11-29 11:21:59 +010052 u64 dax_part_off;
Gao Xiangdfeab2e2021-10-14 16:10:10 +080053
54 u32 blocks;
55 u32 mapped_blkaddr;
56};
57
Huang Jianan40452ff2021-12-06 22:35:52 +080058enum {
59 EROFS_SYNC_DECOMPRESS_AUTO,
60 EROFS_SYNC_DECOMPRESS_FORCE_ON,
61 EROFS_SYNC_DECOMPRESS_FORCE_OFF
62};
63
Gao Xiange6242462021-10-07 15:02:23 +080064struct erofs_mount_opts {
Chao Yuf57a3fe2020-05-29 18:48:36 +080065#ifdef CONFIG_EROFS_FS_ZIP
66 /* current strategy of how to use managed cache */
67 unsigned char cache_strategy;
Huang Jianan40452ff2021-12-06 22:35:52 +080068 /* strategy of sync decompression (0 - auto, 1 - force on, 2 - force off) */
69 unsigned int sync_decompress;
Chao Yuf57a3fe2020-05-29 18:48:36 +080070
71 /* threshold for decompression synchronously */
72 unsigned int max_sync_decompress_pages;
73#endif
74 unsigned int mount_opt;
75};
76
Gao Xiangdfeab2e2021-10-14 16:10:10 +080077struct erofs_dev_context {
78 struct idr tree;
79 struct rw_semaphore rwsem;
80
81 unsigned int extra_devices;
Jia Zhu8b465fe2023-03-02 15:17:51 +080082 bool flatdev;
Gao Xiangdfeab2e2021-10-14 16:10:10 +080083};
84
Gao Xiange6242462021-10-07 15:02:23 +080085struct erofs_fs_context {
86 struct erofs_mount_opts opt;
Gao Xiangdfeab2e2021-10-14 16:10:10 +080087 struct erofs_dev_context *devs;
Jingbo Xu39bfcb82022-10-21 10:31:53 +080088 char *fsid;
89 char *domain_id;
Gao Xiange6242462021-10-07 15:02:23 +080090};
91
Huang Jianan5d505382021-03-29 09:23:06 +080092/* all filesystem-wide lz4 configurations */
93struct erofs_sb_lz4_info {
94 /* # of pages needed for EROFS lz4 rolling decompression */
95 u16 max_distance_pages;
Gao Xiang4fea63f2021-04-07 12:39:23 +080096 /* maximum possible blocks for pclusters in the filesystem */
97 u16 max_pclusterblks;
Huang Jianan5d505382021-03-29 09:23:06 +080098};
99
Jia Zhu8b7adf12022-09-18 12:34:53 +0800100struct erofs_domain {
101 refcount_t ref;
102 struct list_head list;
103 struct fscache_volume *volume;
104 char *domain_id;
105};
106
Jeffle Xub02c6022022-04-25 20:21:34 +0800107struct erofs_fscache {
108 struct fscache_cookie *cookie;
Jingbo Xu61fef982023-02-09 14:39:13 +0800109 struct inode *inode; /* anonymous inode for the blob */
Jingbo Xu2dfb8c32023-02-09 14:39:11 +0800110
111 /* used for share domain mode */
Jia Zhu7d419632022-09-18 19:01:50 +0800112 struct erofs_domain *domain;
Jingbo Xu2dfb8c32023-02-09 14:39:11 +0800113 struct list_head node;
114 refcount_t ref;
Jia Zhu7d419632022-09-18 19:01:50 +0800115 char *name;
Jeffle Xub02c6022022-04-25 20:21:34 +0800116};
117
Jingbo Xu9e382912023-04-07 22:17:08 +0800118struct erofs_xattr_prefix_item {
119 struct erofs_xattr_long_prefix *prefix;
120 u8 infix_len;
121};
122
Gao Xiangbfb86742018-07-26 20:21:45 +0800123struct erofs_sb_info {
Gao Xiange6242462021-10-07 15:02:23 +0800124 struct erofs_mount_opts opt; /* options */
Gao Xiang22fe04a2019-07-31 23:57:39 +0800125#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang2497ee42018-07-26 20:22:03 +0800126 /* list for all registered superblocks, mainly for shrinker */
127 struct list_head list;
Gao Xianga1581312018-07-26 20:22:04 +0800128 struct mutex umount_mutex;
Gao Xiang2497ee42018-07-26 20:22:03 +0800129
Gao Xiang64094a02020-02-20 10:46:42 +0800130 /* managed XArray arranged in physical block number */
131 struct xarray managed_pslots;
Gao Xiang22fe04a2019-07-31 23:57:39 +0800132
Gao Xiang22fe04a2019-07-31 23:57:39 +0800133 unsigned int shrinker_run_no;
Gao Xiang14373712021-03-29 18:00:12 +0800134 u16 available_compr_algs;
Gao Xiang22fe04a2019-07-31 23:57:39 +0800135
Gao Xiang4279f3f2019-07-31 23:57:49 +0800136 /* pseudo inode to manage cached pages */
137 struct inode *managed_cache;
Huang Jianan5d505382021-03-29 09:23:06 +0800138
139 struct erofs_sb_lz4_info lz4;
Gao Xiang22fe04a2019-07-31 23:57:39 +0800140#endif /* CONFIG_EROFS_FS_ZIP */
Jingbo Xua97a2182023-04-07 22:17:06 +0800141 struct inode *packed_inode;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800142 struct erofs_dev_context *devs;
Gao Xiang06252e92021-08-05 08:36:00 +0800143 struct dax_device *dax_dev;
Christoph Hellwigcd913c72021-11-29 11:21:59 +0100144 u64 dax_part_off;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800145 u64 total_blocks;
146 u32 primarydevice_blocks;
147
Gao Xiangbfb86742018-07-26 20:21:45 +0800148 u32 meta_blkaddr;
Gao Xiangb17500a2018-07-26 20:21:52 +0800149#ifdef CONFIG_EROFS_FS_XATTR
150 u32 xattr_blkaddr;
Jingbo Xu9e382912023-04-07 22:17:08 +0800151 u32 xattr_prefix_start;
152 u8 xattr_prefix_count;
153 struct erofs_xattr_prefix_item *xattr_prefixes;
Gao Xiangb17500a2018-07-26 20:21:52 +0800154#endif
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800155 u16 device_id_mask; /* valid bits of device id to be used */
Gao Xiangbfb86742018-07-26 20:21:45 +0800156
Jingbo Xu3acea5f2023-03-13 21:53:08 +0800157 unsigned char islotbits; /* inode slot unit size in bit shift */
Jingbo Xud3c4bdc2023-03-13 21:53:09 +0800158 unsigned char blkszbits; /* filesystem block size in bit shift */
Gao Xiangbfb86742018-07-26 20:21:45 +0800159
Gao Xiang14373712021-03-29 18:00:12 +0800160 u32 sb_size; /* total superblock size */
Gao Xiangbfb86742018-07-26 20:21:45 +0800161 u32 build_time_nsec;
162 u64 build_time;
163
164 /* what we really care is nid, rather than ino.. */
165 erofs_nid_t root_nid;
Jingbo Xucb9bce72023-04-07 22:17:05 +0800166 erofs_nid_t packed_nid;
Gao Xiangbfb86742018-07-26 20:21:45 +0800167 /* used for statfs, f_files - f_favail */
168 u64 inos;
169
170 u8 uuid[16]; /* 128-bit uuid for volume */
171 u8 volume_name[16]; /* volume name */
Pratik Shindeb858a482019-11-04 10:49:37 +0800172 u32 feature_compat;
Gao Xiang426a9302019-09-04 10:08:53 +0800173 u32 feature_incompat;
Huang Jianan168e9a72021-12-01 22:54:36 +0800174
175 /* sysfs support */
176 struct kobject s_kobj; /* /sys/fs/erofs/<devname> */
177 struct completion s_kobj_unregister;
Jeffle Xuc6be2bd2022-04-25 20:21:33 +0800178
179 /* fscache support */
180 struct fscache_volume *volume;
Jeffle Xu37c90c52022-04-25 20:21:37 +0800181 struct erofs_fscache *s_fscache;
Jia Zhu8b7adf12022-09-18 12:34:53 +0800182 struct erofs_domain *domain;
Jingbo Xu39bfcb82022-10-21 10:31:53 +0800183 char *fsid;
184 char *domain_id;
Gao Xiangbfb86742018-07-26 20:21:45 +0800185};
186
187#define EROFS_SB(sb) ((struct erofs_sb_info *)(sb)->s_fs_info)
188#define EROFS_I_SB(inode) ((struct erofs_sb_info *)(inode)->i_sb->s_fs_info)
189
Gao Xiangb17500a2018-07-26 20:21:52 +0800190/* Mount flags set via mount options or defaults */
191#define EROFS_MOUNT_XATTR_USER 0x00000010
192#define EROFS_MOUNT_POSIX_ACL 0x00000020
Gao Xiang06252e92021-08-05 08:36:00 +0800193#define EROFS_MOUNT_DAX_ALWAYS 0x00000040
194#define EROFS_MOUNT_DAX_NEVER 0x00000080
Gao Xiangb17500a2018-07-26 20:21:52 +0800195
Gao Xiange6242462021-10-07 15:02:23 +0800196#define clear_opt(opt, option) ((opt)->mount_opt &= ~EROFS_MOUNT_##option)
197#define set_opt(opt, option) ((opt)->mount_opt |= EROFS_MOUNT_##option)
198#define test_opt(opt, option) ((opt)->mount_opt & EROFS_MOUNT_##option)
Gao Xiangbfb86742018-07-26 20:21:45 +0800199
Jeffle Xu93b856b2022-04-25 20:21:32 +0800200static inline bool erofs_is_fscache_mode(struct super_block *sb)
201{
202 return IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && !sb->s_bdev;
203}
204
Gao Xiang4279f3f2019-07-31 23:57:49 +0800205enum {
206 EROFS_ZIP_CACHE_DISABLED,
207 EROFS_ZIP_CACHE_READAHEAD,
208 EROFS_ZIP_CACHE_READAROUND
209};
210
Gao Xiang14f362b2019-07-31 23:57:36 +0800211#define EROFS_LOCKED_MAGIC (INT_MIN | 0xE0F510CCL)
212
Gao Xiange7e9a302018-07-26 20:22:05 +0800213/* basic unit of the workstation of a super_block */
214struct erofs_workgroup {
215 /* the workgroup index in the workstation */
216 pgoff_t index;
217
218 /* overall workgroup reference count */
219 atomic_t refcount;
220};
221
Gao Xiang73f5c662018-11-23 01:16:02 +0800222static inline bool erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
223 int val)
Gao Xiange7e9a302018-07-26 20:22:05 +0800224{
Gao Xiange7e9a302018-07-26 20:22:05 +0800225 preempt_disable();
Gao Xiang73f5c662018-11-23 01:16:02 +0800226 if (val != atomic_cmpxchg(&grp->refcount, val, EROFS_LOCKED_MAGIC)) {
Gao Xiange7e9a302018-07-26 20:22:05 +0800227 preempt_enable();
228 return false;
229 }
Gao Xiange7e9a302018-07-26 20:22:05 +0800230 return true;
231}
232
Gao Xiang73f5c662018-11-23 01:16:02 +0800233static inline void erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
234 int orig_val)
Gao Xiange7e9a302018-07-26 20:22:05 +0800235{
Gao Xiang948bbdb2018-11-23 01:16:03 +0800236 /*
237 * other observers should notice all modifications
238 * in the freezing period.
239 */
240 smp_mb();
Gao Xiang73f5c662018-11-23 01:16:02 +0800241 atomic_set(&grp->refcount, orig_val);
Gao Xiange7e9a302018-07-26 20:22:05 +0800242 preempt_enable();
243}
244
Gao Xiangdf134b82018-11-23 01:16:01 +0800245static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
246{
247 return atomic_cond_read_relaxed(&grp->refcount,
248 VAL != EROFS_LOCKED_MAGIC);
249}
Gao Xiange7e9a302018-07-26 20:22:05 +0800250
Gao Xiangfdf80a42022-01-02 12:00:13 +0800251enum erofs_kmap_type {
252 EROFS_NO_KMAP, /* don't map the buffer */
Gao Xiang927e5012022-10-18 18:53:13 +0800253 EROFS_KMAP, /* use kmap_local_page() to map the buffer */
Gao Xiangfdf80a42022-01-02 12:00:13 +0800254};
255
256struct erofs_buf {
Gao Xiangeb2c5e42023-04-07 22:17:04 +0800257 struct inode *inode;
Gao Xiangfdf80a42022-01-02 12:00:13 +0800258 struct page *page;
259 void *base;
260 enum erofs_kmap_type kmap_type;
261};
262#define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL })
263
Gao Xiangbfb86742018-07-26 20:21:45 +0800264#define ROOT_NID(sb) ((sb)->root_nid)
265
Jingbo Xu3acea5f2023-03-13 21:53:08 +0800266#define erofs_blknr(sb, addr) ((addr) >> (sb)->s_blocksize_bits)
267#define erofs_blkoff(sb, addr) ((addr) & ((sb)->s_blocksize - 1))
268#define erofs_pos(sb, blk) ((erofs_off_t)(blk) << (sb)->s_blocksize_bits)
269#define erofs_iblks(i) (round_up((i)->i_size, i_blocksize(i)) >> (i)->i_blkbits)
Gao Xiangbfb86742018-07-26 20:21:45 +0800270
Gao Xiangde06a6a2021-03-29 09:23:05 +0800271#define EROFS_FEATURE_FUNCS(name, compat, feature) \
272static inline bool erofs_sb_has_##name(struct erofs_sb_info *sbi) \
273{ \
274 return sbi->feature_##compat & EROFS_FEATURE_##feature; \
275}
276
Huang Jianan7e508f22021-11-13 00:09:33 +0800277EROFS_FEATURE_FUNCS(zero_padding, incompat, INCOMPAT_ZERO_PADDING)
Gao Xiang14373712021-03-29 18:00:12 +0800278EROFS_FEATURE_FUNCS(compr_cfgs, incompat, INCOMPAT_COMPR_CFGS)
Gao Xiang5404c33012021-04-07 12:39:22 +0800279EROFS_FEATURE_FUNCS(big_pcluster, incompat, INCOMPAT_BIG_PCLUSTER)
Huang Jianan168e9a72021-12-01 22:54:36 +0800280EROFS_FEATURE_FUNCS(chunked_file, incompat, INCOMPAT_CHUNKED_FILE)
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800281EROFS_FEATURE_FUNCS(device_table, incompat, INCOMPAT_DEVICE_TABLE)
Huang Jianan168e9a72021-12-01 22:54:36 +0800282EROFS_FEATURE_FUNCS(compr_head2, incompat, INCOMPAT_COMPR_HEAD2)
Yue Huab921842021-12-28 13:46:04 +0800283EROFS_FEATURE_FUNCS(ztailpacking, incompat, INCOMPAT_ZTAILPACKING)
Yue Hub15b2e32022-09-23 10:11:22 +0800284EROFS_FEATURE_FUNCS(fragments, incompat, INCOMPAT_FRAGMENTS)
Gao Xiang5c2a6422022-09-23 09:49:15 +0800285EROFS_FEATURE_FUNCS(dedupe, incompat, INCOMPAT_DEDUPE)
Jingbo Xu6a318cc2023-04-08 06:28:08 +0800286EROFS_FEATURE_FUNCS(xattr_prefixes, incompat, INCOMPAT_XATTR_PREFIXES)
Gao Xiangde06a6a2021-03-29 09:23:05 +0800287EROFS_FEATURE_FUNCS(sb_chksum, compat, COMPAT_SB_CHKSUM)
288
Gao Xiang62dc4592019-02-18 15:19:04 +0800289/* atomic flag definitions */
Gao Xianga5876e22019-09-04 10:08:56 +0800290#define EROFS_I_EA_INITED_BIT 0
291#define EROFS_I_Z_INITED_BIT 1
Gao Xiang62dc4592019-02-18 15:19:04 +0800292
293/* bitlock definitions (arranged in reverse order) */
Gao Xianga5876e22019-09-04 10:08:56 +0800294#define EROFS_I_BL_XATTR_BIT (BITS_PER_LONG - 1)
295#define EROFS_I_BL_Z_BIT (BITS_PER_LONG - 2)
Gao Xiangbfb86742018-07-26 20:21:45 +0800296
Gao Xianga5876e22019-09-04 10:08:56 +0800297struct erofs_inode {
Gao Xiangbfb86742018-07-26 20:21:45 +0800298 erofs_nid_t nid;
Gao Xiang62dc4592019-02-18 15:19:04 +0800299
300 /* atomic flags (including bitlocks) */
301 unsigned long flags;
Gao Xiangbfb86742018-07-26 20:21:45 +0800302
Gao Xiang8a765682019-09-04 10:08:54 +0800303 unsigned char datalayout;
Gao Xiangbfb86742018-07-26 20:21:45 +0800304 unsigned char inode_isize;
Jingbo Xu1b3567a2023-04-14 14:18:10 +0800305 unsigned int xattr_isize;
Gao Xiangbfb86742018-07-26 20:21:45 +0800306
Pratik Shindee82a9a12019-07-15 17:51:27 +0530307 unsigned int xattr_shared_count;
308 unsigned int *xattr_shared_xattrs;
Gao Xiangbfb86742018-07-26 20:21:45 +0800309
Gao Xiang152a3332019-06-24 15:22:52 +0800310 union {
311 erofs_blk_t raw_blkaddr;
Gao Xiangc5aa9032021-08-20 18:00:19 +0800312 struct {
313 unsigned short chunkformat;
314 unsigned char chunkbits;
315 };
Gao Xiang152a3332019-06-24 15:22:52 +0800316#ifdef CONFIG_EROFS_FS_ZIP
317 struct {
318 unsigned short z_advise;
319 unsigned char z_algorithmtype[2];
320 unsigned char z_logical_clusterbits;
Yue Huab921842021-12-28 13:46:04 +0800321 unsigned long z_tailextent_headlcn;
Yue Hub15b2e32022-09-23 10:11:22 +0800322 union {
323 struct {
324 erofs_off_t z_idataoff;
325 unsigned short z_idata_size;
326 };
327 erofs_off_t z_fragmentoff;
328 };
Gao Xiang152a3332019-06-24 15:22:52 +0800329 };
Gao Xiang14f362b2019-07-31 23:57:36 +0800330#endif /* CONFIG_EROFS_FS_ZIP */
Gao Xiang152a3332019-06-24 15:22:52 +0800331 };
Gao Xiangbfb86742018-07-26 20:21:45 +0800332 /* the corresponding vfs inode */
333 struct inode vfs_inode;
334};
335
Gao Xiangb780d3f2023-01-14 23:08:23 +0800336#define EROFS_I(ptr) container_of(ptr, struct erofs_inode, vfs_inode)
Gao Xiangbfb86742018-07-26 20:21:45 +0800337
Gao Xiangb780d3f2023-01-14 23:08:23 +0800338static inline erofs_off_t erofs_iloc(struct inode *inode)
Gao Xiangbfb86742018-07-26 20:21:45 +0800339{
Gao Xiangb780d3f2023-01-14 23:08:23 +0800340 struct erofs_sb_info *sbi = EROFS_I_SB(inode);
341
Jingbo Xu3acea5f2023-03-13 21:53:08 +0800342 return erofs_pos(inode->i_sb, sbi->meta_blkaddr) +
Gao Xiangb780d3f2023-01-14 23:08:23 +0800343 (EROFS_I(inode)->nid << sbi->islotbits);
Gao Xiangbfb86742018-07-26 20:21:45 +0800344}
345
Gao Xiang745ed7d2023-04-14 16:30:27 +0800346static inline unsigned int erofs_inode_version(unsigned int ifmt)
Gao Xiangbfb86742018-07-26 20:21:45 +0800347{
Gao Xiang745ed7d2023-04-14 16:30:27 +0800348 return (ifmt >> EROFS_I_VERSION_BIT) & EROFS_I_VERSION_MASK;
Gao Xiangbfb86742018-07-26 20:21:45 +0800349}
350
Gao Xiang745ed7d2023-04-14 16:30:27 +0800351static inline unsigned int erofs_inode_datalayout(unsigned int ifmt)
Gao Xiangbfb86742018-07-26 20:21:45 +0800352{
Gao Xiang745ed7d2023-04-14 16:30:27 +0800353 return (ifmt >> EROFS_I_DATALAYOUT_BIT) & EROFS_I_DATALAYOUT_MASK;
Gao Xiangbfb86742018-07-26 20:21:45 +0800354}
355
Gao Xiang38629292021-10-09 04:08:39 +0800356/*
357 * Different from grab_cache_page_nowait(), reclaiming is never triggered
358 * when allocating new pages.
359 */
360static inline
361struct page *erofs_grab_cache_page_nowait(struct address_space *mapping,
362 pgoff_t index)
363{
364 return pagecache_get_page(mapping, index,
365 FGP_LOCK|FGP_CREAT|FGP_NOFS|FGP_NOWAIT,
366 readahead_gfp_mask(mapping) & ~__GFP_RECLAIM);
367}
368
Gao Xiangbfb86742018-07-26 20:21:45 +0800369/* Has a disk mapping */
Gao Xiang768bb102023-01-13 14:52:26 +0800370#define EROFS_MAP_MAPPED 0x0001
Gao Xiangbfb86742018-07-26 20:21:45 +0800371/* Located in metadata (could be copied from bd_inode) */
Gao Xiang768bb102023-01-13 14:52:26 +0800372#define EROFS_MAP_META 0x0002
Gao Xiang8f899262021-10-09 04:08:37 +0800373/* The extent is encoded */
Gao Xiang768bb102023-01-13 14:52:26 +0800374#define EROFS_MAP_ENCODED 0x0004
Gao Xiangb6a76182019-06-24 15:22:58 +0800375/* The length of extent is full */
Gao Xiang768bb102023-01-13 14:52:26 +0800376#define EROFS_MAP_FULL_MAPPED 0x0008
Yue Hub15b2e32022-09-23 10:11:22 +0800377/* Located in the special packed inode */
Gao Xiang768bb102023-01-13 14:52:26 +0800378#define EROFS_MAP_FRAGMENT 0x0010
Gao Xiang5c2a6422022-09-23 09:49:15 +0800379/* The extent refers to partial decompressed data */
Gao Xiang768bb102023-01-13 14:52:26 +0800380#define EROFS_MAP_PARTIAL_REF 0x0020
Gao Xiangbfb86742018-07-26 20:21:45 +0800381
382struct erofs_map_blocks {
Gao Xiang09c54372022-01-02 12:00:17 +0800383 struct erofs_buf buf;
384
Gao Xiangbfb86742018-07-26 20:21:45 +0800385 erofs_off_t m_pa, m_la;
386 u64 m_plen, m_llen;
387
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800388 unsigned short m_deviceid;
Gao Xiang8f899262021-10-09 04:08:37 +0800389 char m_algorithmformat;
Gao Xiangbfb86742018-07-26 20:21:45 +0800390 unsigned int m_flags;
391};
392
Gao Xiangd95ae5e2021-08-18 23:22:31 +0800393/*
394 * Used to get the exact decompressed length, e.g. fiemap (consider lookback
395 * approach instead if possible since it's more metadata lightweight.)
396 */
Jingbo Xu8b58f9f2023-02-09 10:48:25 +0800397#define EROFS_GET_BLOCKS_FIEMAP 0x0001
Gao Xiang622cead2021-10-11 05:31:45 +0800398/* Used to map the whole extent if non-negligible data is requested for LZMA */
Jingbo Xu8b58f9f2023-02-09 10:48:25 +0800399#define EROFS_GET_BLOCKS_READMORE 0x0002
Yue Hub15b2e32022-09-23 10:11:22 +0800400/* Used to map tail extent for tailpacking inline or fragment pcluster */
Jingbo Xu8b58f9f2023-02-09 10:48:25 +0800401#define EROFS_GET_BLOCKS_FINDTAIL 0x0004
Gao Xiangbfb86742018-07-26 20:21:45 +0800402
Gao Xiang8f899262021-10-09 04:08:37 +0800403enum {
404 Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
Yue Hufdffc092022-09-23 10:11:21 +0800405 Z_EROFS_COMPRESSION_INTERLACED,
Gao Xiang8f899262021-10-09 04:08:37 +0800406 Z_EROFS_COMPRESSION_RUNTIME_MAX
407};
408
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800409struct erofs_map_dev {
Jeffle Xu955b4782022-04-25 20:21:38 +0800410 struct erofs_fscache *m_fscache;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800411 struct block_device *m_bdev;
412 struct dax_device *m_daxdev;
Christoph Hellwigde205112021-11-29 11:22:00 +0100413 u64 m_dax_part_off;
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800414
415 erofs_off_t m_pa;
416 unsigned int m_deviceid;
417};
418
Gao Xiang557afdd2023-02-04 17:30:40 +0800419extern struct file_system_type erofs_fs_type;
420extern const struct super_operations erofs_sops;
421
422extern const struct address_space_operations erofs_raw_access_aops;
423extern const struct address_space_operations z_erofs_aops;
424extern const struct address_space_operations erofs_fscache_access_aops;
425
426extern const struct inode_operations erofs_generic_iops;
427extern const struct inode_operations erofs_symlink_iops;
428extern const struct inode_operations erofs_fast_symlink_iops;
429extern const struct inode_operations erofs_dir_iops;
430
Huang Jianana08e67a2021-08-05 08:35:59 +0800431extern const struct file_operations erofs_file_fops;
Gao Xiang557afdd2023-02-04 17:30:40 +0800432extern const struct file_operations erofs_dir_fops;
433
434extern const struct iomap_ops z_erofs_iomap_report_ops;
435
436/* flags for erofs_fscache_register_cookie() */
Jingbo Xu61fef982023-02-09 14:39:13 +0800437#define EROFS_REG_COOKIE_SHARE 0x0001
438#define EROFS_REG_COOKIE_NEED_NOEXIST 0x0002
Gao Xiang557afdd2023-02-04 17:30:40 +0800439
Jingbo Xu9e382912023-04-07 22:17:08 +0800440void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
441 erofs_off_t *offset, int *lengthp);
Gao Xiang09c54372022-01-02 12:00:17 +0800442void erofs_unmap_metabuf(struct erofs_buf *buf);
Gao Xiangc521e3a2022-01-02 12:00:14 +0800443void erofs_put_metabuf(struct erofs_buf *buf);
Gao Xiangeb2c5e42023-04-07 22:17:04 +0800444void *erofs_bread(struct erofs_buf *buf, erofs_blk_t blkaddr,
445 enum erofs_kmap_type type);
446void erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb);
Gao Xiangc521e3a2022-01-02 12:00:14 +0800447void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
448 erofs_blk_t blkaddr, enum erofs_kmap_type type);
Gao Xiangdfeab2e2021-10-14 16:10:10 +0800449int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *dev);
Gao Xiangeadcd6b2021-08-13 13:29:31 +0800450int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
451 u64 start, u64 len);
Jingbo Xu8b58f9f2023-02-09 10:48:25 +0800452int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map);
Gao Xiang312fe642022-09-27 14:36:07 +0800453struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid);
Christian Braunerb74d24f2023-01-13 12:49:12 +0100454int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
Christian Brauner549c7292021-01-21 14:19:43 +0100455 struct kstat *stat, u32 request_mask,
456 unsigned int query_flags);
Hongnan Li3e917cc2022-04-25 12:07:12 +0800457int erofs_namei(struct inode *dir, const struct qstr *name,
Gao Xiang60939822019-01-14 19:40:24 +0800458 erofs_nid_t *nid, unsigned int *d_type);
459
Gao Xiang598162d2021-04-07 12:39:26 +0800460static inline void *erofs_vm_map_ram(struct page **pages, unsigned int count)
461{
462 int retried = 0;
463
464 while (1) {
465 void *p = vm_map_ram(pages, count, -1);
466
467 /* retry two more times (totally 3 times) */
468 if (p || ++retried >= 3)
469 return p;
470 vm_unmap_aliases();
471 }
472 return NULL;
473}
474
Huang Jianan168e9a72021-12-01 22:54:36 +0800475int erofs_register_sysfs(struct super_block *sb);
476void erofs_unregister_sysfs(struct super_block *sb);
477int __init erofs_init_sysfs(void);
478void erofs_exit_sysfs(void);
479
Gao Xiangeaa91722021-10-22 17:01:20 +0800480struct page *erofs_allocpage(struct page **pagepool, gfp_t gfp);
Gao Xiang557afdd2023-02-04 17:30:40 +0800481static inline void erofs_pagepool_add(struct page **pagepool, struct page *page)
Gao Xiangeaa91722021-10-22 17:01:20 +0800482{
483 set_page_private(page, (unsigned long)*pagepool);
484 *pagepool = page;
485}
486void erofs_release_pages(struct page **pagepool);
Gao Xiangfa61a332019-06-24 15:22:53 +0800487
Gao Xiang22fe04a2019-07-31 23:57:39 +0800488#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang14f362b2019-07-31 23:57:36 +0800489int erofs_workgroup_put(struct erofs_workgroup *grp);
490struct erofs_workgroup *erofs_find_workgroup(struct super_block *sb,
Vladimir Zapolskiy997626d2020-01-02 14:01:16 +0200491 pgoff_t index);
Gao Xiang64094a02020-02-20 10:46:42 +0800492struct erofs_workgroup *erofs_insert_workgroup(struct super_block *sb,
493 struct erofs_workgroup *grp);
Gao Xiang14f362b2019-07-31 23:57:36 +0800494void erofs_workgroup_free_rcu(struct erofs_workgroup *grp);
Gao Xiang22fe04a2019-07-31 23:57:39 +0800495void erofs_shrinker_register(struct super_block *sb);
496void erofs_shrinker_unregister(struct super_block *sb);
497int __init erofs_init_shrinker(void);
498void erofs_exit_shrinker(void);
499int __init z_erofs_init_zip_subsystem(void);
500void z_erofs_exit_zip_subsystem(void);
Gao Xiang14f362b2019-07-31 23:57:36 +0800501int erofs_try_to_free_all_cached_pages(struct erofs_sb_info *sbi,
502 struct erofs_workgroup *egrp);
Yue Hud252ff32021-08-10 15:24:16 +0800503int erofs_try_to_free_cached_page(struct page *page);
Huang Jianan5d505382021-03-29 09:23:06 +0800504int z_erofs_load_lz4_config(struct super_block *sb,
Gao Xiang46249cd2021-03-29 09:23:07 +0800505 struct erofs_super_block *dsb,
506 struct z_erofs_lz4_cfgs *lz4, int len);
Gao Xiang557afdd2023-02-04 17:30:40 +0800507int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
508 int flags);
Yue Hu285d0f82023-05-15 17:57:58 +0800509void *erofs_get_pcpubuf(unsigned int requiredpages);
510void erofs_put_pcpubuf(void *ptr);
511int erofs_pcpubuf_growsize(unsigned int nrpages);
512void __init erofs_pcpubuf_init(void);
513void erofs_pcpubuf_exit(void);
Gao Xiang22fe04a2019-07-31 23:57:39 +0800514#else
515static inline void erofs_shrinker_register(struct super_block *sb) {}
516static inline void erofs_shrinker_unregister(struct super_block *sb) {}
517static inline int erofs_init_shrinker(void) { return 0; }
518static inline void erofs_exit_shrinker(void) {}
519static inline int z_erofs_init_zip_subsystem(void) { return 0; }
520static inline void z_erofs_exit_zip_subsystem(void) {}
Huang Jianan5d505382021-03-29 09:23:06 +0800521static inline int z_erofs_load_lz4_config(struct super_block *sb,
Gao Xiang46249cd2021-03-29 09:23:07 +0800522 struct erofs_super_block *dsb,
523 struct z_erofs_lz4_cfgs *lz4, int len)
Huang Jianan5d505382021-03-29 09:23:06 +0800524{
Gao Xiang14373712021-03-29 18:00:12 +0800525 if (lz4 || dsb->u1.lz4_max_distance) {
Huang Jianan5d505382021-03-29 09:23:06 +0800526 erofs_err(sb, "lz4 algorithm isn't enabled");
527 return -EINVAL;
528 }
529 return 0;
530}
Yue Hu285d0f82023-05-15 17:57:58 +0800531static inline void erofs_pcpubuf_init(void) {}
532static inline void erofs_pcpubuf_exit(void) {}
Gao Xiang22fe04a2019-07-31 23:57:39 +0800533#endif /* !CONFIG_EROFS_FS_ZIP */
Gao Xiang2e1d6632019-01-16 16:59:56 +0800534
Gao Xiang622cead2021-10-11 05:31:45 +0800535#ifdef CONFIG_EROFS_FS_ZIP_LZMA
Yangtao Lia279aded2023-03-03 14:37:31 +0800536int __init z_erofs_lzma_init(void);
Gao Xiang622cead2021-10-11 05:31:45 +0800537void z_erofs_lzma_exit(void);
538int z_erofs_load_lzma_config(struct super_block *sb,
539 struct erofs_super_block *dsb,
540 struct z_erofs_lzma_cfgs *lzma, int size);
541#else
542static inline int z_erofs_lzma_init(void) { return 0; }
543static inline int z_erofs_lzma_exit(void) { return 0; }
544static inline int z_erofs_load_lzma_config(struct super_block *sb,
545 struct erofs_super_block *dsb,
546 struct z_erofs_lzma_cfgs *lzma, int size) {
547 if (lzma) {
548 erofs_err(sb, "lzma algorithm isn't enabled");
549 return -EINVAL;
550 }
551 return 0;
552}
Gao Xiang557afdd2023-02-04 17:30:40 +0800553#endif /* !CONFIG_EROFS_FS_ZIP_LZMA */
Gao Xiang622cead2021-10-11 05:31:45 +0800554
Jeffle Xuc6be2bd2022-04-25 20:21:33 +0800555#ifdef CONFIG_EROFS_FS_ONDEMAND
556int erofs_fscache_register_fs(struct super_block *sb);
557void erofs_fscache_unregister_fs(struct super_block *sb);
Jeffle Xub02c6022022-04-25 20:21:34 +0800558
Jia Zhue1de2da2022-09-18 12:34:52 +0800559struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
Gao Xiang557afdd2023-02-04 17:30:40 +0800560 char *name, unsigned int flags);
Jia Zhue1de2da2022-09-18 12:34:52 +0800561void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache);
Jeffle Xuc6be2bd2022-04-25 20:21:33 +0800562#else
563static inline int erofs_fscache_register_fs(struct super_block *sb)
564{
Jia Zhue1de2da2022-09-18 12:34:52 +0800565 return -EOPNOTSUPP;
Jeffle Xuc6be2bd2022-04-25 20:21:33 +0800566}
567static inline void erofs_fscache_unregister_fs(struct super_block *sb) {}
Jeffle Xub02c6022022-04-25 20:21:34 +0800568
Jia Zhue1de2da2022-09-18 12:34:52 +0800569static inline
570struct erofs_fscache *erofs_fscache_register_cookie(struct super_block *sb,
Gao Xiang557afdd2023-02-04 17:30:40 +0800571 char *name, unsigned int flags)
Jeffle Xub02c6022022-04-25 20:21:34 +0800572{
Jia Zhue1de2da2022-09-18 12:34:52 +0800573 return ERR_PTR(-EOPNOTSUPP);
Jeffle Xub02c6022022-04-25 20:21:34 +0800574}
575
Jia Zhue1de2da2022-09-18 12:34:52 +0800576static inline void erofs_fscache_unregister_cookie(struct erofs_fscache *fscache)
Jeffle Xub02c6022022-04-25 20:21:34 +0800577{
578}
Jeffle Xuc6be2bd2022-04-25 20:21:33 +0800579#endif
580
Gao Xianga6b9b1d2019-08-14 18:37:03 +0800581#define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
582
Gao Xiang14f362b2019-07-31 23:57:36 +0800583#endif /* __EROFS_INTERNAL_H */