blob: bbf3bbd908e0816e4d4d894a69431c58f0c95b3f [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001// SPDX-License-Identifier: GPL-2.0-only
Gao Xiangba2b77a2018-07-26 20:21:46 +08002/*
Gao Xiangba2b77a2018-07-26 20:21:46 +08003 * Copyright (C) 2017-2018 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiangba2b77a2018-07-26 20:21:46 +08005 * Created by Gao Xiang <gaoxiang25@huawei.com>
Gao Xiangba2b77a2018-07-26 20:21:46 +08006 */
7#include <linux/module.h>
8#include <linux/buffer_head.h>
9#include <linux/statfs.h>
10#include <linux/parser.h>
Gao Xiangb17500a2018-07-26 20:21:52 +080011#include <linux/seq_file.h>
Pratik Shindeb858a482019-11-04 10:49:37 +080012#include <linux/crc32c.h>
Chao Yuf57a3fe2020-05-29 18:48:36 +080013#include <linux/fs_context.h>
14#include <linux/fs_parser.h>
Gao Xiang6af7b482019-01-14 19:40:25 +080015#include "xattr.h"
Gao Xiangba2b77a2018-07-26 20:21:46 +080016
Chao Yu13f06f42018-07-26 20:21:55 +080017#define CREATE_TRACE_POINTS
18#include <trace/events/erofs.h>
19
Gao Xiangba2b77a2018-07-26 20:21:46 +080020static struct kmem_cache *erofs_inode_cachep __read_mostly;
21
Gao Xiang4f761fa2019-09-04 10:09:09 +080022void _erofs_err(struct super_block *sb, const char *function,
23 const char *fmt, ...)
24{
25 struct va_format vaf;
26 va_list args;
27
28 va_start(args, fmt);
29
30 vaf.fmt = fmt;
31 vaf.va = &args;
32
33 pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
34 va_end(args);
35}
36
37void _erofs_info(struct super_block *sb, const char *function,
38 const char *fmt, ...)
39{
40 struct va_format vaf;
41 va_list args;
42
43 va_start(args, fmt);
44
45 vaf.fmt = fmt;
46 vaf.va = &args;
47
48 pr_info("(device %s): %pV", sb->s_id, &vaf);
49 va_end(args);
50}
51
Pratik Shindeb858a482019-11-04 10:49:37 +080052static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
53{
54 struct erofs_super_block *dsb;
55 u32 expected_crc, crc;
56
57 dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET,
58 EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL);
59 if (!dsb)
60 return -ENOMEM;
61
62 expected_crc = le32_to_cpu(dsb->checksum);
63 dsb->checksum = 0;
64 /* to allow for x86 boot sectors and other oddities. */
65 crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET);
66 kfree(dsb);
67
68 if (crc != expected_crc) {
69 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
70 crc, expected_crc);
71 return -EBADMSG;
72 }
73 return 0;
74}
75
Gao Xiang99634bf2019-09-04 10:09:05 +080076static void erofs_inode_init_once(void *ptr)
Gao Xiangba2b77a2018-07-26 20:21:46 +080077{
Gao Xianga5876e22019-09-04 10:08:56 +080078 struct erofs_inode *vi = ptr;
Gao Xiangba2b77a2018-07-26 20:21:46 +080079
80 inode_init_once(&vi->vfs_inode);
81}
82
Gao Xiang99634bf2019-09-04 10:09:05 +080083static struct inode *erofs_alloc_inode(struct super_block *sb)
Gao Xiangba2b77a2018-07-26 20:21:46 +080084{
Gao Xianga5876e22019-09-04 10:08:56 +080085 struct erofs_inode *vi =
Gao Xiangba2b77a2018-07-26 20:21:46 +080086 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
87
Vatsala Narange2ff9f12019-03-21 05:36:13 +053088 if (!vi)
Gao Xiangba2b77a2018-07-26 20:21:46 +080089 return NULL;
90
91 /* zero out everything except vfs_inode */
Gao Xianga5876e22019-09-04 10:08:56 +080092 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
Gao Xiangba2b77a2018-07-26 20:21:46 +080093 return &vi->vfs_inode;
94}
95
Gao Xiang99634bf2019-09-04 10:09:05 +080096static void erofs_free_inode(struct inode *inode)
Gao Xiangba2b77a2018-07-26 20:21:46 +080097{
Gao Xianga5876e22019-09-04 10:08:56 +080098 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiangba2b77a2018-07-26 20:21:46 +080099
Gao Xianga2c75c82019-09-04 10:08:59 +0800100 /* be careful of RCU symlink path */
101 if (inode->i_op == &erofs_fast_symlink_iops)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800102 kfree(inode->i_link);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800103 kfree(vi->xattr_shared_xattrs);
104
105 kmem_cache_free(erofs_inode_cachep, vi);
106}
107
Gao Xiang5efe5132019-06-13 16:35:41 +0800108static bool check_layout_compatibility(struct super_block *sb,
Gao Xiang0259f202019-09-04 10:09:00 +0800109 struct erofs_super_block *dsb)
Gao Xiang5efe5132019-06-13 16:35:41 +0800110{
Gao Xiang0259f202019-09-04 10:09:00 +0800111 const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
Gao Xiang5efe5132019-06-13 16:35:41 +0800112
Gao Xiang426a9302019-09-04 10:08:53 +0800113 EROFS_SB(sb)->feature_incompat = feature;
Gao Xiang5efe5132019-06-13 16:35:41 +0800114
115 /* check if current kernel meets all mandatory requirements */
Gao Xiang426a9302019-09-04 10:08:53 +0800116 if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800117 erofs_err(sb,
118 "unidentified incompatible feature %x, please upgrade kernel version",
119 feature & ~EROFS_ALL_FEATURE_INCOMPAT);
Gao Xiang5efe5132019-06-13 16:35:41 +0800120 return false;
121 }
122 return true;
123}
124
Gao Xiang14373712021-03-29 18:00:12 +0800125#ifdef CONFIG_EROFS_FS_ZIP
126/* read variable-sized metadata, offset will be aligned by 4-byte */
127static void *erofs_read_metadata(struct super_block *sb, struct page **pagep,
128 erofs_off_t *offset, int *lengthp)
129{
130 struct page *page = *pagep;
131 u8 *buffer, *ptr;
132 int len, i, cnt;
133 erofs_blk_t blk;
134
135 *offset = round_up(*offset, 4);
136 blk = erofs_blknr(*offset);
137
138 if (!page || page->index != blk) {
139 if (page) {
140 unlock_page(page);
141 put_page(page);
142 }
143 page = erofs_get_meta_page(sb, blk);
144 if (IS_ERR(page))
145 goto err_nullpage;
146 }
147
148 ptr = kmap(page);
149 len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(*offset)]);
150 if (!len)
151 len = U16_MAX + 1;
152 buffer = kmalloc(len, GFP_KERNEL);
153 if (!buffer) {
154 buffer = ERR_PTR(-ENOMEM);
155 goto out;
156 }
157 *offset += sizeof(__le16);
158 *lengthp = len;
159
160 for (i = 0; i < len; i += cnt) {
161 cnt = min(EROFS_BLKSIZ - (int)erofs_blkoff(*offset), len - i);
162 blk = erofs_blknr(*offset);
163
164 if (!page || page->index != blk) {
165 if (page) {
166 kunmap(page);
167 unlock_page(page);
168 put_page(page);
169 }
170 page = erofs_get_meta_page(sb, blk);
171 if (IS_ERR(page)) {
172 kfree(buffer);
173 goto err_nullpage;
174 }
175 ptr = kmap(page);
176 }
177 memcpy(buffer + i, ptr + erofs_blkoff(*offset), cnt);
178 *offset += cnt;
179 }
180out:
181 kunmap(page);
182 *pagep = page;
183 return buffer;
184err_nullpage:
185 *pagep = NULL;
186 return page;
187}
188
189static int erofs_load_compr_cfgs(struct super_block *sb,
190 struct erofs_super_block *dsb)
191{
192 struct erofs_sb_info *sbi;
193 struct page *page;
194 unsigned int algs, alg;
195 erofs_off_t offset;
196 int size, ret;
197
198 sbi = EROFS_SB(sb);
199 sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
200
201 if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
202 erofs_err(sb, "try to load compressed fs with unsupported algorithms %x",
203 sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
204 return -EINVAL;
205 }
206
207 offset = EROFS_SUPER_OFFSET + sbi->sb_size;
208 page = NULL;
209 alg = 0;
210 ret = 0;
211
212 for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
213 void *data;
214
215 if (!(algs & 1))
216 continue;
217
218 data = erofs_read_metadata(sb, &page, &offset, &size);
219 if (IS_ERR(data)) {
220 ret = PTR_ERR(data);
221 goto err;
222 }
223
224 switch (alg) {
225 case Z_EROFS_COMPRESSION_LZ4:
226 ret = z_erofs_load_lz4_config(sb, dsb, data, size);
227 break;
228 default:
229 DBG_BUGON(1);
230 ret = -EFAULT;
231 }
232 kfree(data);
233 if (ret)
234 goto err;
235 }
236err:
237 if (page) {
238 unlock_page(page);
239 put_page(page);
240 }
241 return ret;
242}
243#else
244static int erofs_load_compr_cfgs(struct super_block *sb,
245 struct erofs_super_block *dsb)
246{
247 if (dsb->u1.available_compr_algs) {
248 erofs_err(sb, "try to load compressed fs when compression is disabled");
249 return -EINVAL;
250 }
251 return 0;
252}
253#endif
254
Gao Xiang99634bf2019-09-04 10:09:05 +0800255static int erofs_read_superblock(struct super_block *sb)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800256{
257 struct erofs_sb_info *sbi;
Gao Xiangfe7c2422019-09-04 10:09:10 +0800258 struct page *page;
Gao Xiang0259f202019-09-04 10:09:00 +0800259 struct erofs_super_block *dsb;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200260 unsigned int blkszbits;
Gao Xiangfe7c2422019-09-04 10:09:10 +0800261 void *data;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800262 int ret;
263
Gao Xiangfe7c2422019-09-04 10:09:10 +0800264 page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL);
Wei Yongjun517d6b92019-09-18 08:30:33 +0000265 if (IS_ERR(page)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800266 erofs_err(sb, "cannot read erofs superblock");
Wei Yongjun517d6b92019-09-18 08:30:33 +0000267 return PTR_ERR(page);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800268 }
269
270 sbi = EROFS_SB(sb);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800271
Pratik Shindeb858a482019-11-04 10:49:37 +0800272 data = kmap(page);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800273 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800274
275 ret = -EINVAL;
Gao Xiang0259f202019-09-04 10:09:00 +0800276 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800277 erofs_err(sb, "cannot find valid erofs superblock");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800278 goto out;
279 }
280
Pratik Shindeb858a482019-11-04 10:49:37 +0800281 sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
Gao Xiangde06a6a2021-03-29 09:23:05 +0800282 if (erofs_sb_has_sb_chksum(sbi)) {
Pratik Shindeb858a482019-11-04 10:49:37 +0800283 ret = erofs_superblock_csum_verify(sb, data);
284 if (ret)
285 goto out;
286 }
287
Gao Xiang0259f202019-09-04 10:09:00 +0800288 blkszbits = dsb->blkszbits;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800289 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800290 if (blkszbits != LOG_BLOCK_SIZE) {
Gao Xiangbde54522021-01-20 09:30:16 +0800291 erofs_err(sb, "blkszbits %u isn't supported on this platform",
292 blkszbits);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800293 goto out;
294 }
295
Gao Xiang0259f202019-09-04 10:09:00 +0800296 if (!check_layout_compatibility(sb, dsb))
Gao Xiang5efe5132019-06-13 16:35:41 +0800297 goto out;
298
Gao Xiang14373712021-03-29 18:00:12 +0800299 sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
300 if (sbi->sb_size > EROFS_BLKSIZ) {
301 erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
302 sbi->sb_size);
303 goto out;
304 }
Gao Xiang0259f202019-09-04 10:09:00 +0800305 sbi->blocks = le32_to_cpu(dsb->blocks);
306 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
Gao Xiangb17500a2018-07-26 20:21:52 +0800307#ifdef CONFIG_EROFS_FS_XATTR
Gao Xiang0259f202019-09-04 10:09:00 +0800308 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
Gao Xiangb17500a2018-07-26 20:21:52 +0800309#endif
Gao Xiang8a765682019-09-04 10:08:54 +0800310 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
Gao Xiang0259f202019-09-04 10:09:00 +0800311 sbi->root_nid = le16_to_cpu(dsb->root_nid);
312 sbi->inos = le64_to_cpu(dsb->inos);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800313
Gao Xiang0259f202019-09-04 10:09:00 +0800314 sbi->build_time = le64_to_cpu(dsb->build_time);
315 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800316
Gao Xiang0259f202019-09-04 10:09:00 +0800317 memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800318
Gao Xiang0259f202019-09-04 10:09:00 +0800319 ret = strscpy(sbi->volume_name, dsb->volume_name,
320 sizeof(dsb->volume_name));
Gao Xianga64d9492019-08-18 18:28:24 +0800321 if (ret < 0) { /* -E2BIG */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800322 erofs_err(sb, "bad volume name without NIL terminator");
Gao Xianga64d9492019-08-18 18:28:24 +0800323 ret = -EFSCORRUPTED;
324 goto out;
325 }
Huang Jianan5d505382021-03-29 09:23:06 +0800326
327 /* parse on-disk compression configurations */
Gao Xiang14373712021-03-29 18:00:12 +0800328 if (erofs_sb_has_compr_cfgs(sbi))
329 ret = erofs_load_compr_cfgs(sb, dsb);
330 else
331 ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800332out:
Pratik Shindeb858a482019-11-04 10:49:37 +0800333 kunmap(page);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800334 put_page(page);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800335 return ret;
336}
337
Gao Xiang4279f3f2019-07-31 23:57:49 +0800338/* set up default EROFS parameters */
Chao Yuf57a3fe2020-05-29 18:48:36 +0800339static void erofs_default_options(struct erofs_fs_context *ctx)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800340{
341#ifdef CONFIG_EROFS_FS_ZIP
Chao Yuf57a3fe2020-05-29 18:48:36 +0800342 ctx->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
343 ctx->max_sync_decompress_pages = 3;
Huang Jianan30048cd2021-03-17 11:54:48 +0800344 ctx->readahead_sync_decompress = false;
Gao Xiang4279f3f2019-07-31 23:57:49 +0800345#endif
Gao Xiangb17500a2018-07-26 20:21:52 +0800346#ifdef CONFIG_EROFS_FS_XATTR
Chao Yuf57a3fe2020-05-29 18:48:36 +0800347 set_opt(ctx, XATTR_USER);
Gao Xiangb17500a2018-07-26 20:21:52 +0800348#endif
Gao Xiangb17500a2018-07-26 20:21:52 +0800349#ifdef CONFIG_EROFS_FS_POSIX_ACL
Chao Yuf57a3fe2020-05-29 18:48:36 +0800350 set_opt(ctx, POSIX_ACL);
Gao Xiangb17500a2018-07-26 20:21:52 +0800351#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800352}
353
354enum {
Gao Xiangb17500a2018-07-26 20:21:52 +0800355 Opt_user_xattr,
Gao Xiangb17500a2018-07-26 20:21:52 +0800356 Opt_acl,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800357 Opt_cache_strategy,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800358 Opt_err
359};
360
Chao Yuf57a3fe2020-05-29 18:48:36 +0800361static const struct constant_table erofs_param_cache_strategy[] = {
362 {"disabled", EROFS_ZIP_CACHE_DISABLED},
363 {"readahead", EROFS_ZIP_CACHE_READAHEAD},
364 {"readaround", EROFS_ZIP_CACHE_READAROUND},
365 {}
Gao Xiangba2b77a2018-07-26 20:21:46 +0800366};
367
Chao Yuf57a3fe2020-05-29 18:48:36 +0800368static const struct fs_parameter_spec erofs_fs_parameters[] = {
369 fsparam_flag_no("user_xattr", Opt_user_xattr),
370 fsparam_flag_no("acl", Opt_acl),
371 fsparam_enum("cache_strategy", Opt_cache_strategy,
372 erofs_param_cache_strategy),
373 {}
374};
375
376static int erofs_fc_parse_param(struct fs_context *fc,
377 struct fs_parameter *param)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800378{
Chao Yuf57a3fe2020-05-29 18:48:36 +0800379 struct erofs_fs_context *ctx __maybe_unused = fc->fs_private;
380 struct fs_parse_result result;
381 int opt;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800382
Chao Yuf57a3fe2020-05-29 18:48:36 +0800383 opt = fs_parse(fc, erofs_fs_parameters, param, &result);
384 if (opt < 0)
385 return opt;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800386
Chao Yuf57a3fe2020-05-29 18:48:36 +0800387 switch (opt) {
388 case Opt_user_xattr:
Gao Xiangb17500a2018-07-26 20:21:52 +0800389#ifdef CONFIG_EROFS_FS_XATTR
Chao Yuf57a3fe2020-05-29 18:48:36 +0800390 if (result.boolean)
391 set_opt(ctx, XATTR_USER);
392 else
393 clear_opt(ctx, XATTR_USER);
Gao Xiangb17500a2018-07-26 20:21:52 +0800394#else
Chao Yuf57a3fe2020-05-29 18:48:36 +0800395 errorfc(fc, "{,no}user_xattr options not supported");
Gao Xiangb17500a2018-07-26 20:21:52 +0800396#endif
Chao Yuf57a3fe2020-05-29 18:48:36 +0800397 break;
398 case Opt_acl:
Gao Xiangb17500a2018-07-26 20:21:52 +0800399#ifdef CONFIG_EROFS_FS_POSIX_ACL
Chao Yuf57a3fe2020-05-29 18:48:36 +0800400 if (result.boolean)
401 set_opt(ctx, POSIX_ACL);
402 else
403 clear_opt(ctx, POSIX_ACL);
Gao Xiangb17500a2018-07-26 20:21:52 +0800404#else
Chao Yuf57a3fe2020-05-29 18:48:36 +0800405 errorfc(fc, "{,no}acl options not supported");
Gao Xiangb17500a2018-07-26 20:21:52 +0800406#endif
Chao Yuf57a3fe2020-05-29 18:48:36 +0800407 break;
408 case Opt_cache_strategy:
409#ifdef CONFIG_EROFS_FS_ZIP
410 ctx->cache_strategy = result.uint_32;
411#else
412 errorfc(fc, "compression not supported, cache_strategy ignored");
413#endif
414 break;
415 default:
416 return -ENOPARAM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800417 }
418 return 0;
419}
420
Gao Xiang4279f3f2019-07-31 23:57:49 +0800421#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang105d4ad2018-07-26 20:22:07 +0800422static const struct address_space_operations managed_cache_aops;
423
Gao Xiang99634bf2019-09-04 10:09:05 +0800424static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800425{
426 int ret = 1; /* 0 - busy */
427 struct address_space *const mapping = page->mapping;
428
Gao Xiang8b987bc2018-12-05 21:23:13 +0800429 DBG_BUGON(!PageLocked(page));
430 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800431
432 if (PagePrivate(page))
Gao Xiang47e541a2018-07-29 13:34:58 +0800433 ret = erofs_try_to_free_cached_page(mapping, page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800434
435 return ret;
436}
437
Gao Xiang99634bf2019-09-04 10:09:05 +0800438static void erofs_managed_cache_invalidatepage(struct page *page,
439 unsigned int offset,
440 unsigned int length)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800441{
442 const unsigned int stop = length + offset;
443
Gao Xiang8b987bc2018-12-05 21:23:13 +0800444 DBG_BUGON(!PageLocked(page));
Gao Xiang105d4ad2018-07-26 20:22:07 +0800445
Gao Xiang8b987bc2018-12-05 21:23:13 +0800446 /* Check for potential overflow in debug mode */
447 DBG_BUGON(stop > PAGE_SIZE || stop < length);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800448
449 if (offset == 0 && stop == PAGE_SIZE)
Gao Xiang99634bf2019-09-04 10:09:05 +0800450 while (!erofs_managed_cache_releasepage(page, GFP_NOFS))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800451 cond_resched();
452}
453
454static const struct address_space_operations managed_cache_aops = {
Gao Xiang99634bf2019-09-04 10:09:05 +0800455 .releasepage = erofs_managed_cache_releasepage,
456 .invalidatepage = erofs_managed_cache_invalidatepage,
Gao Xiang105d4ad2018-07-26 20:22:07 +0800457};
458
Gao Xiang8f7acda2019-07-31 23:57:41 +0800459static int erofs_init_managed_cache(struct super_block *sb)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800460{
Gao Xiang8f7acda2019-07-31 23:57:41 +0800461 struct erofs_sb_info *const sbi = EROFS_SB(sb);
462 struct inode *const inode = new_inode(sb);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800463
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800464 if (!inode)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800465 return -ENOMEM;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800466
467 set_nlink(inode, 1);
468 inode->i_size = OFFSET_MAX;
469
470 inode->i_mapping->a_ops = &managed_cache_aops;
471 mapping_set_gfp_mask(inode->i_mapping,
Gao Xiang8494c292019-07-31 23:57:42 +0800472 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800473 sbi->managed_cache = inode;
474 return 0;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800475}
Gao Xiang8f7acda2019-07-31 23:57:41 +0800476#else
477static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800478#endif
479
Chao Yuf57a3fe2020-05-29 18:48:36 +0800480static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800481{
482 struct inode *inode;
483 struct erofs_sb_info *sbi;
Chao Yuf57a3fe2020-05-29 18:48:36 +0800484 struct erofs_fs_context *ctx = fc->fs_private;
Gao Xiang8f7acda2019-07-31 23:57:41 +0800485 int err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800486
Gao Xiang8f7acda2019-07-31 23:57:41 +0800487 sb->s_magic = EROFS_SUPER_MAGIC;
488
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800489 if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800490 erofs_err(sb, "failed to set erofs blksize");
Gao Xiang8f7acda2019-07-31 23:57:41 +0800491 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800492 }
493
Shobhit Kukretia9f69bd2019-06-26 22:31:18 -0700494 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800495 if (!sbi)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800496 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800497
Gao Xiang8f7acda2019-07-31 23:57:41 +0800498 sb->s_fs_info = sbi;
Gao Xiang99634bf2019-09-04 10:09:05 +0800499 err = erofs_read_superblock(sb);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800500 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800501 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800502
Gao Xiang5f0abea2018-09-06 17:01:47 +0800503 sb->s_flags |= SB_RDONLY | SB_NOATIME;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800504 sb->s_maxbytes = MAX_LFS_FILESIZE;
505 sb->s_time_gran = 1;
506
507 sb->s_op = &erofs_sops;
Gao Xiangb17500a2018-07-26 20:21:52 +0800508 sb->s_xattr = erofs_xattr_handlers;
Chengguang Xue7cda1e2020-05-26 17:03:43 +0800509
Chao Yuf57a3fe2020-05-29 18:48:36 +0800510 if (test_opt(ctx, POSIX_ACL))
Gao Xiang516c115c2019-01-29 16:35:20 +0800511 sb->s_flags |= SB_POSIXACL;
512 else
513 sb->s_flags &= ~SB_POSIXACL;
514
Chao Yuf57a3fe2020-05-29 18:48:36 +0800515 sbi->ctx = *ctx;
516
Gao Xiange7e9a302018-07-26 20:22:05 +0800517#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang64094a02020-02-20 10:46:42 +0800518 xa_init(&sbi->managed_pslots);
Gao Xiange7e9a302018-07-26 20:22:05 +0800519#endif
520
Gao Xiangba2b77a2018-07-26 20:21:46 +0800521 /* get the root inode */
522 inode = erofs_iget(sb, ROOT_NID(sbi), true);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800523 if (IS_ERR(inode))
524 return PTR_ERR(inode);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800525
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800526 if (!S_ISDIR(inode->i_mode)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800527 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
528 ROOT_NID(sbi), inode->i_mode);
Chengguang Xu94832d92019-01-23 14:12:25 +0800529 iput(inode);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800530 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800531 }
532
533 sb->s_root = d_make_root(inode);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800534 if (!sb->s_root)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800535 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800536
Gao Xiang22fe04a2019-07-31 23:57:39 +0800537 erofs_shrinker_register(sb);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800538 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
539 err = erofs_init_managed_cache(sb);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800540 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800541 return err;
Gao Xiang2497ee42018-07-26 20:22:03 +0800542
Chao Yuf57a3fe2020-05-29 18:48:36 +0800543 erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800544 return 0;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800545}
546
Chao Yuf57a3fe2020-05-29 18:48:36 +0800547static int erofs_fc_get_tree(struct fs_context *fc)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800548{
Chao Yuf57a3fe2020-05-29 18:48:36 +0800549 return get_tree_bdev(fc, erofs_fc_fill_super);
550}
551
552static int erofs_fc_reconfigure(struct fs_context *fc)
553{
554 struct super_block *sb = fc->root->d_sb;
555 struct erofs_sb_info *sbi = EROFS_SB(sb);
556 struct erofs_fs_context *ctx = fc->fs_private;
557
558 DBG_BUGON(!sb_rdonly(sb));
559
560 if (test_opt(ctx, POSIX_ACL))
561 fc->sb_flags |= SB_POSIXACL;
562 else
563 fc->sb_flags &= ~SB_POSIXACL;
564
565 sbi->ctx = *ctx;
566
567 fc->sb_flags |= SB_RDONLY;
568 return 0;
569}
570
571static void erofs_fc_free(struct fs_context *fc)
572{
573 kfree(fc->fs_private);
574}
575
576static const struct fs_context_operations erofs_context_ops = {
577 .parse_param = erofs_fc_parse_param,
578 .get_tree = erofs_fc_get_tree,
579 .reconfigure = erofs_fc_reconfigure,
580 .free = erofs_fc_free,
581};
582
583static int erofs_init_fs_context(struct fs_context *fc)
584{
585 fc->fs_private = kzalloc(sizeof(struct erofs_fs_context), GFP_KERNEL);
586 if (!fc->fs_private)
587 return -ENOMEM;
588
589 /* set default mount options */
590 erofs_default_options(fc->fs_private);
591
592 fc->ops = &erofs_context_ops;
593
594 return 0;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800595}
596
Gao Xiang8f7acda2019-07-31 23:57:41 +0800597/*
598 * could be triggered after deactivate_locked_super()
599 * is called, thus including umount and failed to initialize.
600 */
601static void erofs_kill_sb(struct super_block *sb)
602{
603 struct erofs_sb_info *sbi;
604
605 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800606
607 kill_block_super(sb);
608
609 sbi = EROFS_SB(sb);
610 if (!sbi)
611 return;
612 kfree(sbi);
613 sb->s_fs_info = NULL;
614}
615
616/* called when ->s_root is non-NULL */
617static void erofs_put_super(struct super_block *sb)
618{
619 struct erofs_sb_info *const sbi = EROFS_SB(sb);
620
621 DBG_BUGON(!sbi);
622
623 erofs_shrinker_unregister(sb);
Gao Xiang4279f3f2019-07-31 23:57:49 +0800624#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang8f7acda2019-07-31 23:57:41 +0800625 iput(sbi->managed_cache);
626 sbi->managed_cache = NULL;
627#endif
628}
629
Gao Xiangba2b77a2018-07-26 20:21:46 +0800630static struct file_system_type erofs_fs_type = {
631 .owner = THIS_MODULE,
632 .name = "erofs",
Chao Yuf57a3fe2020-05-29 18:48:36 +0800633 .init_fs_context = erofs_init_fs_context,
Gao Xiang8f7acda2019-07-31 23:57:41 +0800634 .kill_sb = erofs_kill_sb,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800635 .fs_flags = FS_REQUIRES_DEV,
636};
637MODULE_ALIAS_FS("erofs");
638
639static int __init erofs_module_init(void)
640{
641 int err;
642
643 erofs_check_ondisk_layout_definitions();
Gao Xiangba2b77a2018-07-26 20:21:46 +0800644
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800645 erofs_inode_cachep = kmem_cache_create("erofs_inode",
Gao Xianga5876e22019-09-04 10:08:56 +0800646 sizeof(struct erofs_inode), 0,
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800647 SLAB_RECLAIM_ACCOUNT,
Gao Xiang99634bf2019-09-04 10:09:05 +0800648 erofs_inode_init_once);
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800649 if (!erofs_inode_cachep) {
650 err = -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800651 goto icache_err;
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800652 }
Gao Xiangba2b77a2018-07-26 20:21:46 +0800653
Gao Xiang22fe04a2019-07-31 23:57:39 +0800654 err = erofs_init_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800655 if (err)
656 goto shrinker_err;
657
Gao Xiang52488732021-04-10 03:06:30 +0800658 erofs_pcpubuf_init();
Gao Xiang3883a792018-07-26 20:22:06 +0800659 err = z_erofs_init_zip_subsystem();
660 if (err)
661 goto zip_err;
Gao Xiang3883a792018-07-26 20:22:06 +0800662
Gao Xiangba2b77a2018-07-26 20:21:46 +0800663 err = register_filesystem(&erofs_fs_type);
664 if (err)
665 goto fs_err;
666
Gao Xiangba2b77a2018-07-26 20:21:46 +0800667 return 0;
668
669fs_err:
Gao Xiang3883a792018-07-26 20:22:06 +0800670 z_erofs_exit_zip_subsystem();
671zip_err:
Gao Xiang22fe04a2019-07-31 23:57:39 +0800672 erofs_exit_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800673shrinker_err:
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800674 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800675icache_err:
676 return err;
677}
678
679static void __exit erofs_module_exit(void)
680{
681 unregister_filesystem(&erofs_fs_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800682 z_erofs_exit_zip_subsystem();
Gao Xiang22fe04a2019-07-31 23:57:39 +0800683 erofs_exit_shrinker();
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800684
685 /* Ensure all RCU free inodes are safe before cache is destroyed. */
686 rcu_barrier();
687 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiang52488732021-04-10 03:06:30 +0800688 erofs_pcpubuf_exit();
Gao Xiangba2b77a2018-07-26 20:21:46 +0800689}
690
691/* get filesystem statistics */
692static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
693{
694 struct super_block *sb = dentry->d_sb;
695 struct erofs_sb_info *sbi = EROFS_SB(sb);
696 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
697
698 buf->f_type = sb->s_magic;
699 buf->f_bsize = EROFS_BLKSIZ;
700 buf->f_blocks = sbi->blocks;
701 buf->f_bfree = buf->f_bavail = 0;
702
703 buf->f_files = ULLONG_MAX;
704 buf->f_ffree = ULLONG_MAX - sbi->inos;
705
706 buf->f_namelen = EROFS_NAME_LEN;
707
Al Viro6d1349c2020-09-18 16:45:50 -0400708 buf->f_fsid = u64_to_fsid(id);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800709 return 0;
710}
711
712static int erofs_show_options(struct seq_file *seq, struct dentry *root)
713{
Gao Xiangb17500a2018-07-26 20:21:52 +0800714 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
Chao Yuf57a3fe2020-05-29 18:48:36 +0800715 struct erofs_fs_context *ctx __maybe_unused = &sbi->ctx;
Gao Xiangb17500a2018-07-26 20:21:52 +0800716
717#ifdef CONFIG_EROFS_FS_XATTR
Chao Yuf57a3fe2020-05-29 18:48:36 +0800718 if (test_opt(ctx, XATTR_USER))
Gao Xiangb17500a2018-07-26 20:21:52 +0800719 seq_puts(seq, ",user_xattr");
720 else
721 seq_puts(seq, ",nouser_xattr");
722#endif
723#ifdef CONFIG_EROFS_FS_POSIX_ACL
Chao Yuf57a3fe2020-05-29 18:48:36 +0800724 if (test_opt(ctx, POSIX_ACL))
Gao Xiangb17500a2018-07-26 20:21:52 +0800725 seq_puts(seq, ",acl");
726 else
727 seq_puts(seq, ",noacl");
728#endif
Gao Xiang4279f3f2019-07-31 23:57:49 +0800729#ifdef CONFIG_EROFS_FS_ZIP
Chao Yuf57a3fe2020-05-29 18:48:36 +0800730 if (ctx->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800731 seq_puts(seq, ",cache_strategy=disabled");
Chao Yuf57a3fe2020-05-29 18:48:36 +0800732 else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800733 seq_puts(seq, ",cache_strategy=readahead");
Chao Yuf57a3fe2020-05-29 18:48:36 +0800734 else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800735 seq_puts(seq, ",cache_strategy=readaround");
Gao Xiang4279f3f2019-07-31 23:57:49 +0800736#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800737 return 0;
738}
739
Gao Xiangba2b77a2018-07-26 20:21:46 +0800740const struct super_operations erofs_sops = {
741 .put_super = erofs_put_super,
Gao Xiang99634bf2019-09-04 10:09:05 +0800742 .alloc_inode = erofs_alloc_inode,
743 .free_inode = erofs_free_inode,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800744 .statfs = erofs_statfs,
745 .show_options = erofs_show_options,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800746};
747
748module_init(erofs_module_init);
749module_exit(erofs_module_exit);
750
751MODULE_DESCRIPTION("Enhanced ROM File System");
Gao Xiangbc33d9f2019-07-31 23:57:51 +0800752MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800753MODULE_LICENSE("GPL");
754