blob: be10b16ea66ee53279b10a3741b8e1724af3ecc4 [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 Xiang99634bf2019-09-04 10:09:05 +0800125static int erofs_read_superblock(struct super_block *sb)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800126{
127 struct erofs_sb_info *sbi;
Gao Xiangfe7c2422019-09-04 10:09:10 +0800128 struct page *page;
Gao Xiang0259f202019-09-04 10:09:00 +0800129 struct erofs_super_block *dsb;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200130 unsigned int blkszbits;
Gao Xiangfe7c2422019-09-04 10:09:10 +0800131 void *data;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800132 int ret;
133
Gao Xiangfe7c2422019-09-04 10:09:10 +0800134 page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL);
Wei Yongjun517d6b92019-09-18 08:30:33 +0000135 if (IS_ERR(page)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800136 erofs_err(sb, "cannot read erofs superblock");
Wei Yongjun517d6b92019-09-18 08:30:33 +0000137 return PTR_ERR(page);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800138 }
139
140 sbi = EROFS_SB(sb);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800141
Pratik Shindeb858a482019-11-04 10:49:37 +0800142 data = kmap(page);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800143 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800144
145 ret = -EINVAL;
Gao Xiang0259f202019-09-04 10:09:00 +0800146 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800147 erofs_err(sb, "cannot find valid erofs superblock");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800148 goto out;
149 }
150
Pratik Shindeb858a482019-11-04 10:49:37 +0800151 sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
152 if (sbi->feature_compat & EROFS_FEATURE_COMPAT_SB_CHKSUM) {
153 ret = erofs_superblock_csum_verify(sb, data);
154 if (ret)
155 goto out;
156 }
157
Gao Xiang0259f202019-09-04 10:09:00 +0800158 blkszbits = dsb->blkszbits;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800159 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800160 if (blkszbits != LOG_BLOCK_SIZE) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800161 erofs_err(sb, "blksize %u isn't supported on this platform",
162 1 << blkszbits);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800163 goto out;
164 }
165
Gao Xiang0259f202019-09-04 10:09:00 +0800166 if (!check_layout_compatibility(sb, dsb))
Gao Xiang5efe5132019-06-13 16:35:41 +0800167 goto out;
168
Gao Xiang0259f202019-09-04 10:09:00 +0800169 sbi->blocks = le32_to_cpu(dsb->blocks);
170 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
Gao Xiangb17500a2018-07-26 20:21:52 +0800171#ifdef CONFIG_EROFS_FS_XATTR
Gao Xiang0259f202019-09-04 10:09:00 +0800172 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
Gao Xiangb17500a2018-07-26 20:21:52 +0800173#endif
Gao Xiang8a765682019-09-04 10:08:54 +0800174 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
Gao Xiang0259f202019-09-04 10:09:00 +0800175 sbi->root_nid = le16_to_cpu(dsb->root_nid);
176 sbi->inos = le64_to_cpu(dsb->inos);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800177
Gao Xiang0259f202019-09-04 10:09:00 +0800178 sbi->build_time = le64_to_cpu(dsb->build_time);
179 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800180
Gao Xiang0259f202019-09-04 10:09:00 +0800181 memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800182
Gao Xiang0259f202019-09-04 10:09:00 +0800183 ret = strscpy(sbi->volume_name, dsb->volume_name,
184 sizeof(dsb->volume_name));
Gao Xianga64d9492019-08-18 18:28:24 +0800185 if (ret < 0) { /* -E2BIG */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800186 erofs_err(sb, "bad volume name without NIL terminator");
Gao Xianga64d9492019-08-18 18:28:24 +0800187 ret = -EFSCORRUPTED;
188 goto out;
189 }
Gao Xiangba2b77a2018-07-26 20:21:46 +0800190 ret = 0;
191out:
Pratik Shindeb858a482019-11-04 10:49:37 +0800192 kunmap(page);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800193 put_page(page);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800194 return ret;
195}
196
Gao Xiang4279f3f2019-07-31 23:57:49 +0800197/* set up default EROFS parameters */
Chao Yuf57a3fe2020-05-29 18:48:36 +0800198static void erofs_default_options(struct erofs_fs_context *ctx)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800199{
200#ifdef CONFIG_EROFS_FS_ZIP
Chao Yuf57a3fe2020-05-29 18:48:36 +0800201 ctx->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
202 ctx->max_sync_decompress_pages = 3;
Gao Xiang4279f3f2019-07-31 23:57:49 +0800203#endif
Gao Xiangb17500a2018-07-26 20:21:52 +0800204#ifdef CONFIG_EROFS_FS_XATTR
Chao Yuf57a3fe2020-05-29 18:48:36 +0800205 set_opt(ctx, XATTR_USER);
Gao Xiangb17500a2018-07-26 20:21:52 +0800206#endif
Gao Xiangb17500a2018-07-26 20:21:52 +0800207#ifdef CONFIG_EROFS_FS_POSIX_ACL
Chao Yuf57a3fe2020-05-29 18:48:36 +0800208 set_opt(ctx, POSIX_ACL);
Gao Xiangb17500a2018-07-26 20:21:52 +0800209#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800210}
211
212enum {
Gao Xiangb17500a2018-07-26 20:21:52 +0800213 Opt_user_xattr,
Gao Xiangb17500a2018-07-26 20:21:52 +0800214 Opt_acl,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800215 Opt_cache_strategy,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800216 Opt_err
217};
218
Chao Yuf57a3fe2020-05-29 18:48:36 +0800219static const struct constant_table erofs_param_cache_strategy[] = {
220 {"disabled", EROFS_ZIP_CACHE_DISABLED},
221 {"readahead", EROFS_ZIP_CACHE_READAHEAD},
222 {"readaround", EROFS_ZIP_CACHE_READAROUND},
223 {}
Gao Xiangba2b77a2018-07-26 20:21:46 +0800224};
225
Chao Yuf57a3fe2020-05-29 18:48:36 +0800226static const struct fs_parameter_spec erofs_fs_parameters[] = {
227 fsparam_flag_no("user_xattr", Opt_user_xattr),
228 fsparam_flag_no("acl", Opt_acl),
229 fsparam_enum("cache_strategy", Opt_cache_strategy,
230 erofs_param_cache_strategy),
231 {}
232};
233
234static int erofs_fc_parse_param(struct fs_context *fc,
235 struct fs_parameter *param)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800236{
Chao Yuf57a3fe2020-05-29 18:48:36 +0800237 struct erofs_fs_context *ctx __maybe_unused = fc->fs_private;
238 struct fs_parse_result result;
239 int opt;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800240
Chao Yuf57a3fe2020-05-29 18:48:36 +0800241 opt = fs_parse(fc, erofs_fs_parameters, param, &result);
242 if (opt < 0)
243 return opt;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800244
Chao Yuf57a3fe2020-05-29 18:48:36 +0800245 switch (opt) {
246 case Opt_user_xattr:
Gao Xiangb17500a2018-07-26 20:21:52 +0800247#ifdef CONFIG_EROFS_FS_XATTR
Chao Yuf57a3fe2020-05-29 18:48:36 +0800248 if (result.boolean)
249 set_opt(ctx, XATTR_USER);
250 else
251 clear_opt(ctx, XATTR_USER);
Gao Xiangb17500a2018-07-26 20:21:52 +0800252#else
Chao Yuf57a3fe2020-05-29 18:48:36 +0800253 errorfc(fc, "{,no}user_xattr options not supported");
Gao Xiangb17500a2018-07-26 20:21:52 +0800254#endif
Chao Yuf57a3fe2020-05-29 18:48:36 +0800255 break;
256 case Opt_acl:
Gao Xiangb17500a2018-07-26 20:21:52 +0800257#ifdef CONFIG_EROFS_FS_POSIX_ACL
Chao Yuf57a3fe2020-05-29 18:48:36 +0800258 if (result.boolean)
259 set_opt(ctx, POSIX_ACL);
260 else
261 clear_opt(ctx, POSIX_ACL);
Gao Xiangb17500a2018-07-26 20:21:52 +0800262#else
Chao Yuf57a3fe2020-05-29 18:48:36 +0800263 errorfc(fc, "{,no}acl options not supported");
Gao Xiangb17500a2018-07-26 20:21:52 +0800264#endif
Chao Yuf57a3fe2020-05-29 18:48:36 +0800265 break;
266 case Opt_cache_strategy:
267#ifdef CONFIG_EROFS_FS_ZIP
268 ctx->cache_strategy = result.uint_32;
269#else
270 errorfc(fc, "compression not supported, cache_strategy ignored");
271#endif
272 break;
273 default:
274 return -ENOPARAM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800275 }
276 return 0;
277}
278
Gao Xiang4279f3f2019-07-31 23:57:49 +0800279#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang105d4ad2018-07-26 20:22:07 +0800280static const struct address_space_operations managed_cache_aops;
281
Gao Xiang99634bf2019-09-04 10:09:05 +0800282static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800283{
284 int ret = 1; /* 0 - busy */
285 struct address_space *const mapping = page->mapping;
286
Gao Xiang8b987bc2018-12-05 21:23:13 +0800287 DBG_BUGON(!PageLocked(page));
288 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800289
290 if (PagePrivate(page))
Gao Xiang47e541a2018-07-29 13:34:58 +0800291 ret = erofs_try_to_free_cached_page(mapping, page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800292
293 return ret;
294}
295
Gao Xiang99634bf2019-09-04 10:09:05 +0800296static void erofs_managed_cache_invalidatepage(struct page *page,
297 unsigned int offset,
298 unsigned int length)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800299{
300 const unsigned int stop = length + offset;
301
Gao Xiang8b987bc2018-12-05 21:23:13 +0800302 DBG_BUGON(!PageLocked(page));
Gao Xiang105d4ad2018-07-26 20:22:07 +0800303
Gao Xiang8b987bc2018-12-05 21:23:13 +0800304 /* Check for potential overflow in debug mode */
305 DBG_BUGON(stop > PAGE_SIZE || stop < length);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800306
307 if (offset == 0 && stop == PAGE_SIZE)
Gao Xiang99634bf2019-09-04 10:09:05 +0800308 while (!erofs_managed_cache_releasepage(page, GFP_NOFS))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800309 cond_resched();
310}
311
312static const struct address_space_operations managed_cache_aops = {
Gao Xiang99634bf2019-09-04 10:09:05 +0800313 .releasepage = erofs_managed_cache_releasepage,
314 .invalidatepage = erofs_managed_cache_invalidatepage,
Gao Xiang105d4ad2018-07-26 20:22:07 +0800315};
316
Gao Xiang8f7acda2019-07-31 23:57:41 +0800317static int erofs_init_managed_cache(struct super_block *sb)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800318{
Gao Xiang8f7acda2019-07-31 23:57:41 +0800319 struct erofs_sb_info *const sbi = EROFS_SB(sb);
320 struct inode *const inode = new_inode(sb);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800321
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800322 if (!inode)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800323 return -ENOMEM;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800324
325 set_nlink(inode, 1);
326 inode->i_size = OFFSET_MAX;
327
328 inode->i_mapping->a_ops = &managed_cache_aops;
329 mapping_set_gfp_mask(inode->i_mapping,
Gao Xiang8494c292019-07-31 23:57:42 +0800330 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800331 sbi->managed_cache = inode;
332 return 0;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800333}
Gao Xiang8f7acda2019-07-31 23:57:41 +0800334#else
335static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800336#endif
337
Chao Yuf57a3fe2020-05-29 18:48:36 +0800338static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800339{
340 struct inode *inode;
341 struct erofs_sb_info *sbi;
Chao Yuf57a3fe2020-05-29 18:48:36 +0800342 struct erofs_fs_context *ctx = fc->fs_private;
Gao Xiang8f7acda2019-07-31 23:57:41 +0800343 int err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800344
Gao Xiang8f7acda2019-07-31 23:57:41 +0800345 sb->s_magic = EROFS_SUPER_MAGIC;
346
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800347 if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800348 erofs_err(sb, "failed to set erofs blksize");
Gao Xiang8f7acda2019-07-31 23:57:41 +0800349 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800350 }
351
Shobhit Kukretia9f69bd2019-06-26 22:31:18 -0700352 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800353 if (!sbi)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800354 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800355
Gao Xiang8f7acda2019-07-31 23:57:41 +0800356 sb->s_fs_info = sbi;
Gao Xiang99634bf2019-09-04 10:09:05 +0800357 err = erofs_read_superblock(sb);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800358 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800359 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800360
Gao Xiang5f0abea2018-09-06 17:01:47 +0800361 sb->s_flags |= SB_RDONLY | SB_NOATIME;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800362 sb->s_maxbytes = MAX_LFS_FILESIZE;
363 sb->s_time_gran = 1;
364
365 sb->s_op = &erofs_sops;
Gao Xiangb17500a2018-07-26 20:21:52 +0800366 sb->s_xattr = erofs_xattr_handlers;
Chengguang Xue7cda1e2020-05-26 17:03:43 +0800367
Chao Yuf57a3fe2020-05-29 18:48:36 +0800368 if (test_opt(ctx, POSIX_ACL))
Gao Xiang516c115c2019-01-29 16:35:20 +0800369 sb->s_flags |= SB_POSIXACL;
370 else
371 sb->s_flags &= ~SB_POSIXACL;
372
Chao Yuf57a3fe2020-05-29 18:48:36 +0800373 sbi->ctx = *ctx;
374
Gao Xiange7e9a302018-07-26 20:22:05 +0800375#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang64094a02020-02-20 10:46:42 +0800376 xa_init(&sbi->managed_pslots);
Gao Xiange7e9a302018-07-26 20:22:05 +0800377#endif
378
Gao Xiangba2b77a2018-07-26 20:21:46 +0800379 /* get the root inode */
380 inode = erofs_iget(sb, ROOT_NID(sbi), true);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800381 if (IS_ERR(inode))
382 return PTR_ERR(inode);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800383
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800384 if (!S_ISDIR(inode->i_mode)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800385 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
386 ROOT_NID(sbi), inode->i_mode);
Chengguang Xu94832d92019-01-23 14:12:25 +0800387 iput(inode);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800388 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800389 }
390
391 sb->s_root = d_make_root(inode);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800392 if (!sb->s_root)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800393 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800394
Gao Xiang22fe04a2019-07-31 23:57:39 +0800395 erofs_shrinker_register(sb);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800396 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
397 err = erofs_init_managed_cache(sb);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800398 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800399 return err;
Gao Xiang2497ee42018-07-26 20:22:03 +0800400
Chao Yuf57a3fe2020-05-29 18:48:36 +0800401 erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800402 return 0;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800403}
404
Chao Yuf57a3fe2020-05-29 18:48:36 +0800405static int erofs_fc_get_tree(struct fs_context *fc)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800406{
Chao Yuf57a3fe2020-05-29 18:48:36 +0800407 return get_tree_bdev(fc, erofs_fc_fill_super);
408}
409
410static int erofs_fc_reconfigure(struct fs_context *fc)
411{
412 struct super_block *sb = fc->root->d_sb;
413 struct erofs_sb_info *sbi = EROFS_SB(sb);
414 struct erofs_fs_context *ctx = fc->fs_private;
415
416 DBG_BUGON(!sb_rdonly(sb));
417
418 if (test_opt(ctx, POSIX_ACL))
419 fc->sb_flags |= SB_POSIXACL;
420 else
421 fc->sb_flags &= ~SB_POSIXACL;
422
423 sbi->ctx = *ctx;
424
425 fc->sb_flags |= SB_RDONLY;
426 return 0;
427}
428
429static void erofs_fc_free(struct fs_context *fc)
430{
431 kfree(fc->fs_private);
432}
433
434static const struct fs_context_operations erofs_context_ops = {
435 .parse_param = erofs_fc_parse_param,
436 .get_tree = erofs_fc_get_tree,
437 .reconfigure = erofs_fc_reconfigure,
438 .free = erofs_fc_free,
439};
440
441static int erofs_init_fs_context(struct fs_context *fc)
442{
443 fc->fs_private = kzalloc(sizeof(struct erofs_fs_context), GFP_KERNEL);
444 if (!fc->fs_private)
445 return -ENOMEM;
446
447 /* set default mount options */
448 erofs_default_options(fc->fs_private);
449
450 fc->ops = &erofs_context_ops;
451
452 return 0;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800453}
454
Gao Xiang8f7acda2019-07-31 23:57:41 +0800455/*
456 * could be triggered after deactivate_locked_super()
457 * is called, thus including umount and failed to initialize.
458 */
459static void erofs_kill_sb(struct super_block *sb)
460{
461 struct erofs_sb_info *sbi;
462
463 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800464
465 kill_block_super(sb);
466
467 sbi = EROFS_SB(sb);
468 if (!sbi)
469 return;
470 kfree(sbi);
471 sb->s_fs_info = NULL;
472}
473
474/* called when ->s_root is non-NULL */
475static void erofs_put_super(struct super_block *sb)
476{
477 struct erofs_sb_info *const sbi = EROFS_SB(sb);
478
479 DBG_BUGON(!sbi);
480
481 erofs_shrinker_unregister(sb);
Gao Xiang4279f3f2019-07-31 23:57:49 +0800482#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang8f7acda2019-07-31 23:57:41 +0800483 iput(sbi->managed_cache);
484 sbi->managed_cache = NULL;
485#endif
486}
487
Gao Xiangba2b77a2018-07-26 20:21:46 +0800488static struct file_system_type erofs_fs_type = {
489 .owner = THIS_MODULE,
490 .name = "erofs",
Chao Yuf57a3fe2020-05-29 18:48:36 +0800491 .init_fs_context = erofs_init_fs_context,
Gao Xiang8f7acda2019-07-31 23:57:41 +0800492 .kill_sb = erofs_kill_sb,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800493 .fs_flags = FS_REQUIRES_DEV,
494};
495MODULE_ALIAS_FS("erofs");
496
497static int __init erofs_module_init(void)
498{
499 int err;
500
501 erofs_check_ondisk_layout_definitions();
Gao Xiangba2b77a2018-07-26 20:21:46 +0800502
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800503 erofs_inode_cachep = kmem_cache_create("erofs_inode",
Gao Xianga5876e22019-09-04 10:08:56 +0800504 sizeof(struct erofs_inode), 0,
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800505 SLAB_RECLAIM_ACCOUNT,
Gao Xiang99634bf2019-09-04 10:09:05 +0800506 erofs_inode_init_once);
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800507 if (!erofs_inode_cachep) {
508 err = -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800509 goto icache_err;
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800510 }
Gao Xiangba2b77a2018-07-26 20:21:46 +0800511
Gao Xiang22fe04a2019-07-31 23:57:39 +0800512 err = erofs_init_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800513 if (err)
514 goto shrinker_err;
515
Gao Xiang3883a792018-07-26 20:22:06 +0800516 err = z_erofs_init_zip_subsystem();
517 if (err)
518 goto zip_err;
Gao Xiang3883a792018-07-26 20:22:06 +0800519
Gao Xiangba2b77a2018-07-26 20:21:46 +0800520 err = register_filesystem(&erofs_fs_type);
521 if (err)
522 goto fs_err;
523
Gao Xiangba2b77a2018-07-26 20:21:46 +0800524 return 0;
525
526fs_err:
Gao Xiang3883a792018-07-26 20:22:06 +0800527 z_erofs_exit_zip_subsystem();
528zip_err:
Gao Xiang22fe04a2019-07-31 23:57:39 +0800529 erofs_exit_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800530shrinker_err:
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800531 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800532icache_err:
533 return err;
534}
535
536static void __exit erofs_module_exit(void)
537{
538 unregister_filesystem(&erofs_fs_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800539 z_erofs_exit_zip_subsystem();
Gao Xiang22fe04a2019-07-31 23:57:39 +0800540 erofs_exit_shrinker();
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800541
542 /* Ensure all RCU free inodes are safe before cache is destroyed. */
543 rcu_barrier();
544 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800545}
546
547/* get filesystem statistics */
548static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
549{
550 struct super_block *sb = dentry->d_sb;
551 struct erofs_sb_info *sbi = EROFS_SB(sb);
552 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
553
554 buf->f_type = sb->s_magic;
555 buf->f_bsize = EROFS_BLKSIZ;
556 buf->f_blocks = sbi->blocks;
557 buf->f_bfree = buf->f_bavail = 0;
558
559 buf->f_files = ULLONG_MAX;
560 buf->f_ffree = ULLONG_MAX - sbi->inos;
561
562 buf->f_namelen = EROFS_NAME_LEN;
563
Al Viro6d1349c2020-09-18 16:45:50 -0400564 buf->f_fsid = u64_to_fsid(id);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800565 return 0;
566}
567
568static int erofs_show_options(struct seq_file *seq, struct dentry *root)
569{
Gao Xiangb17500a2018-07-26 20:21:52 +0800570 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
Chao Yuf57a3fe2020-05-29 18:48:36 +0800571 struct erofs_fs_context *ctx __maybe_unused = &sbi->ctx;
Gao Xiangb17500a2018-07-26 20:21:52 +0800572
573#ifdef CONFIG_EROFS_FS_XATTR
Chao Yuf57a3fe2020-05-29 18:48:36 +0800574 if (test_opt(ctx, XATTR_USER))
Gao Xiangb17500a2018-07-26 20:21:52 +0800575 seq_puts(seq, ",user_xattr");
576 else
577 seq_puts(seq, ",nouser_xattr");
578#endif
579#ifdef CONFIG_EROFS_FS_POSIX_ACL
Chao Yuf57a3fe2020-05-29 18:48:36 +0800580 if (test_opt(ctx, POSIX_ACL))
Gao Xiangb17500a2018-07-26 20:21:52 +0800581 seq_puts(seq, ",acl");
582 else
583 seq_puts(seq, ",noacl");
584#endif
Gao Xiang4279f3f2019-07-31 23:57:49 +0800585#ifdef CONFIG_EROFS_FS_ZIP
Chao Yuf57a3fe2020-05-29 18:48:36 +0800586 if (ctx->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800587 seq_puts(seq, ",cache_strategy=disabled");
Chao Yuf57a3fe2020-05-29 18:48:36 +0800588 else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800589 seq_puts(seq, ",cache_strategy=readahead");
Chao Yuf57a3fe2020-05-29 18:48:36 +0800590 else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800591 seq_puts(seq, ",cache_strategy=readaround");
Gao Xiang4279f3f2019-07-31 23:57:49 +0800592#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800593 return 0;
594}
595
Gao Xiangba2b77a2018-07-26 20:21:46 +0800596const struct super_operations erofs_sops = {
597 .put_super = erofs_put_super,
Gao Xiang99634bf2019-09-04 10:09:05 +0800598 .alloc_inode = erofs_alloc_inode,
599 .free_inode = erofs_free_inode,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800600 .statfs = erofs_statfs,
601 .show_options = erofs_show_options,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800602};
603
604module_init(erofs_module_init);
605module_exit(erofs_module_exit);
606
607MODULE_DESCRIPTION("Enhanced ROM File System");
Gao Xiangbc33d9f2019-07-31 23:57:51 +0800608MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800609MODULE_LICENSE("GPL");
610