blob: 8e46d204a0c21fd2da35129b706ca1f62cf12fed [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.
4 * http://www.huawei.com/
5 * 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>
Gao Xiang6af7b482019-01-14 19:40:25 +080013#include "xattr.h"
Gao Xiangba2b77a2018-07-26 20:21:46 +080014
Chao Yu13f06f42018-07-26 20:21:55 +080015#define CREATE_TRACE_POINTS
16#include <trace/events/erofs.h>
17
Gao Xiangba2b77a2018-07-26 20:21:46 +080018static struct kmem_cache *erofs_inode_cachep __read_mostly;
19
Gao Xiang4f761fa2019-09-04 10:09:09 +080020void _erofs_err(struct super_block *sb, const char *function,
21 const char *fmt, ...)
22{
23 struct va_format vaf;
24 va_list args;
25
26 va_start(args, fmt);
27
28 vaf.fmt = fmt;
29 vaf.va = &args;
30
31 pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
32 va_end(args);
33}
34
35void _erofs_info(struct super_block *sb, const char *function,
36 const char *fmt, ...)
37{
38 struct va_format vaf;
39 va_list args;
40
41 va_start(args, fmt);
42
43 vaf.fmt = fmt;
44 vaf.va = &args;
45
46 pr_info("(device %s): %pV", sb->s_id, &vaf);
47 va_end(args);
48}
49
Pratik Shindeb858a482019-11-04 10:49:37 +080050static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
51{
52 struct erofs_super_block *dsb;
53 u32 expected_crc, crc;
54
55 dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET,
56 EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL);
57 if (!dsb)
58 return -ENOMEM;
59
60 expected_crc = le32_to_cpu(dsb->checksum);
61 dsb->checksum = 0;
62 /* to allow for x86 boot sectors and other oddities. */
63 crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET);
64 kfree(dsb);
65
66 if (crc != expected_crc) {
67 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
68 crc, expected_crc);
69 return -EBADMSG;
70 }
71 return 0;
72}
73
Gao Xiang99634bf2019-09-04 10:09:05 +080074static void erofs_inode_init_once(void *ptr)
Gao Xiangba2b77a2018-07-26 20:21:46 +080075{
Gao Xianga5876e22019-09-04 10:08:56 +080076 struct erofs_inode *vi = ptr;
Gao Xiangba2b77a2018-07-26 20:21:46 +080077
78 inode_init_once(&vi->vfs_inode);
79}
80
Gao Xiang99634bf2019-09-04 10:09:05 +080081static struct inode *erofs_alloc_inode(struct super_block *sb)
Gao Xiangba2b77a2018-07-26 20:21:46 +080082{
Gao Xianga5876e22019-09-04 10:08:56 +080083 struct erofs_inode *vi =
Gao Xiangba2b77a2018-07-26 20:21:46 +080084 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
85
Vatsala Narange2ff9f12019-03-21 05:36:13 +053086 if (!vi)
Gao Xiangba2b77a2018-07-26 20:21:46 +080087 return NULL;
88
89 /* zero out everything except vfs_inode */
Gao Xianga5876e22019-09-04 10:08:56 +080090 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
Gao Xiangba2b77a2018-07-26 20:21:46 +080091 return &vi->vfs_inode;
92}
93
Gao Xiang99634bf2019-09-04 10:09:05 +080094static void erofs_free_inode(struct inode *inode)
Gao Xiangba2b77a2018-07-26 20:21:46 +080095{
Gao Xianga5876e22019-09-04 10:08:56 +080096 struct erofs_inode *vi = EROFS_I(inode);
Gao Xiangba2b77a2018-07-26 20:21:46 +080097
Gao Xianga2c75c82019-09-04 10:08:59 +080098 /* be careful of RCU symlink path */
99 if (inode->i_op == &erofs_fast_symlink_iops)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800100 kfree(inode->i_link);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800101 kfree(vi->xattr_shared_xattrs);
102
103 kmem_cache_free(erofs_inode_cachep, vi);
104}
105
Gao Xiang5efe5132019-06-13 16:35:41 +0800106static bool check_layout_compatibility(struct super_block *sb,
Gao Xiang0259f202019-09-04 10:09:00 +0800107 struct erofs_super_block *dsb)
Gao Xiang5efe5132019-06-13 16:35:41 +0800108{
Gao Xiang0259f202019-09-04 10:09:00 +0800109 const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
Gao Xiang5efe5132019-06-13 16:35:41 +0800110
Gao Xiang426a9302019-09-04 10:08:53 +0800111 EROFS_SB(sb)->feature_incompat = feature;
Gao Xiang5efe5132019-06-13 16:35:41 +0800112
113 /* check if current kernel meets all mandatory requirements */
Gao Xiang426a9302019-09-04 10:08:53 +0800114 if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800115 erofs_err(sb,
116 "unidentified incompatible feature %x, please upgrade kernel version",
117 feature & ~EROFS_ALL_FEATURE_INCOMPAT);
Gao Xiang5efe5132019-06-13 16:35:41 +0800118 return false;
119 }
120 return true;
121}
122
Gao Xiang99634bf2019-09-04 10:09:05 +0800123static int erofs_read_superblock(struct super_block *sb)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800124{
125 struct erofs_sb_info *sbi;
Gao Xiangfe7c2422019-09-04 10:09:10 +0800126 struct page *page;
Gao Xiang0259f202019-09-04 10:09:00 +0800127 struct erofs_super_block *dsb;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +0200128 unsigned int blkszbits;
Gao Xiangfe7c2422019-09-04 10:09:10 +0800129 void *data;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800130 int ret;
131
Gao Xiangfe7c2422019-09-04 10:09:10 +0800132 page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL);
Wei Yongjun517d6b92019-09-18 08:30:33 +0000133 if (IS_ERR(page)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800134 erofs_err(sb, "cannot read erofs superblock");
Wei Yongjun517d6b92019-09-18 08:30:33 +0000135 return PTR_ERR(page);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800136 }
137
138 sbi = EROFS_SB(sb);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800139
Pratik Shindeb858a482019-11-04 10:49:37 +0800140 data = kmap(page);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800141 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800142
143 ret = -EINVAL;
Gao Xiang0259f202019-09-04 10:09:00 +0800144 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800145 erofs_err(sb, "cannot find valid erofs superblock");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800146 goto out;
147 }
148
Pratik Shindeb858a482019-11-04 10:49:37 +0800149 sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
150 if (sbi->feature_compat & EROFS_FEATURE_COMPAT_SB_CHKSUM) {
151 ret = erofs_superblock_csum_verify(sb, data);
152 if (ret)
153 goto out;
154 }
155
Gao Xiang0259f202019-09-04 10:09:00 +0800156 blkszbits = dsb->blkszbits;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800157 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800158 if (blkszbits != LOG_BLOCK_SIZE) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800159 erofs_err(sb, "blksize %u isn't supported on this platform",
160 1 << blkszbits);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800161 goto out;
162 }
163
Gao Xiang0259f202019-09-04 10:09:00 +0800164 if (!check_layout_compatibility(sb, dsb))
Gao Xiang5efe5132019-06-13 16:35:41 +0800165 goto out;
166
Gao Xiang0259f202019-09-04 10:09:00 +0800167 sbi->blocks = le32_to_cpu(dsb->blocks);
168 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
Gao Xiangb17500a2018-07-26 20:21:52 +0800169#ifdef CONFIG_EROFS_FS_XATTR
Gao Xiang0259f202019-09-04 10:09:00 +0800170 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
Gao Xiangb17500a2018-07-26 20:21:52 +0800171#endif
Gao Xiang8a765682019-09-04 10:08:54 +0800172 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
Gao Xiang0259f202019-09-04 10:09:00 +0800173 sbi->root_nid = le16_to_cpu(dsb->root_nid);
174 sbi->inos = le64_to_cpu(dsb->inos);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800175
Gao Xiang0259f202019-09-04 10:09:00 +0800176 sbi->build_time = le64_to_cpu(dsb->build_time);
177 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800178
Gao Xiang0259f202019-09-04 10:09:00 +0800179 memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800180
Gao Xiang0259f202019-09-04 10:09:00 +0800181 ret = strscpy(sbi->volume_name, dsb->volume_name,
182 sizeof(dsb->volume_name));
Gao Xianga64d9492019-08-18 18:28:24 +0800183 if (ret < 0) { /* -E2BIG */
Gao Xiang4f761fa2019-09-04 10:09:09 +0800184 erofs_err(sb, "bad volume name without NIL terminator");
Gao Xianga64d9492019-08-18 18:28:24 +0800185 ret = -EFSCORRUPTED;
186 goto out;
187 }
Gao Xiangba2b77a2018-07-26 20:21:46 +0800188 ret = 0;
189out:
Pratik Shindeb858a482019-11-04 10:49:37 +0800190 kunmap(page);
Gao Xiangfe7c2422019-09-04 10:09:10 +0800191 put_page(page);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800192 return ret;
193}
194
Gao Xiang5fb76bb2018-09-20 00:06:56 +0800195#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang4f761fa2019-09-04 10:09:09 +0800196static int erofs_build_cache_strategy(struct super_block *sb,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800197 substring_t *args)
198{
Gao Xiang4f761fa2019-09-04 10:09:09 +0800199 struct erofs_sb_info *sbi = EROFS_SB(sb);
Gao Xiang4279f3f2019-07-31 23:57:49 +0800200 const char *cs = match_strdup(args);
201 int err = 0;
202
203 if (!cs) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800204 erofs_err(sb, "Not enough memory to store cache strategy");
Gao Xiang4279f3f2019-07-31 23:57:49 +0800205 return -ENOMEM;
206 }
207
208 if (!strcmp(cs, "disabled")) {
209 sbi->cache_strategy = EROFS_ZIP_CACHE_DISABLED;
210 } else if (!strcmp(cs, "readahead")) {
211 sbi->cache_strategy = EROFS_ZIP_CACHE_READAHEAD;
212 } else if (!strcmp(cs, "readaround")) {
213 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
214 } else {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800215 erofs_err(sb, "Unrecognized cache strategy \"%s\"", cs);
Gao Xiang4279f3f2019-07-31 23:57:49 +0800216 err = -EINVAL;
217 }
218 kfree(cs);
219 return err;
220}
221#else
Gao Xiang4f761fa2019-09-04 10:09:09 +0800222static int erofs_build_cache_strategy(struct super_block *sb,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800223 substring_t *args)
224{
Gao Xiang4f761fa2019-09-04 10:09:09 +0800225 erofs_info(sb, "EROFS compression is disabled, so cache strategy is ignored");
Gao Xiang4279f3f2019-07-31 23:57:49 +0800226 return 0;
227}
Gao Xiang5fb76bb2018-09-20 00:06:56 +0800228#endif
229
Gao Xiang4279f3f2019-07-31 23:57:49 +0800230/* set up default EROFS parameters */
Gao Xiang99634bf2019-09-04 10:09:05 +0800231static void erofs_default_options(struct erofs_sb_info *sbi)
Gao Xiang4279f3f2019-07-31 23:57:49 +0800232{
233#ifdef CONFIG_EROFS_FS_ZIP
234 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
235 sbi->max_sync_decompress_pages = 3;
236#endif
Gao Xiangb17500a2018-07-26 20:21:52 +0800237#ifdef CONFIG_EROFS_FS_XATTR
238 set_opt(sbi, XATTR_USER);
239#endif
Gao Xiangb17500a2018-07-26 20:21:52 +0800240#ifdef CONFIG_EROFS_FS_POSIX_ACL
241 set_opt(sbi, POSIX_ACL);
242#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800243}
244
245enum {
Gao Xiangb17500a2018-07-26 20:21:52 +0800246 Opt_user_xattr,
247 Opt_nouser_xattr,
248 Opt_acl,
249 Opt_noacl,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800250 Opt_cache_strategy,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800251 Opt_err
252};
253
254static match_table_t erofs_tokens = {
Gao Xiangb17500a2018-07-26 20:21:52 +0800255 {Opt_user_xattr, "user_xattr"},
256 {Opt_nouser_xattr, "nouser_xattr"},
257 {Opt_acl, "acl"},
258 {Opt_noacl, "noacl"},
Gao Xiang4279f3f2019-07-31 23:57:49 +0800259 {Opt_cache_strategy, "cache_strategy=%s"},
Gao Xiangba2b77a2018-07-26 20:21:46 +0800260 {Opt_err, NULL}
261};
262
Gao Xiang99634bf2019-09-04 10:09:05 +0800263static int erofs_parse_options(struct super_block *sb, char *options)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800264{
265 substring_t args[MAX_OPT_ARGS];
266 char *p;
Chengguang Xu01e4ae42018-09-19 22:53:44 +0800267 int err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800268
269 if (!options)
270 return 0;
271
Bhanusree Pola561fb352019-03-22 10:38:16 +0800272 while ((p = strsep(&options, ","))) {
Gao Xiangba2b77a2018-07-26 20:21:46 +0800273 int token;
274
275 if (!*p)
276 continue;
277
278 args[0].to = args[0].from = NULL;
279 token = match_token(p, erofs_tokens, args);
280
281 switch (token) {
Gao Xiangb17500a2018-07-26 20:21:52 +0800282#ifdef CONFIG_EROFS_FS_XATTR
283 case Opt_user_xattr:
284 set_opt(EROFS_SB(sb), XATTR_USER);
285 break;
286 case Opt_nouser_xattr:
287 clear_opt(EROFS_SB(sb), XATTR_USER);
288 break;
289#else
290 case Opt_user_xattr:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800291 erofs_info(sb, "user_xattr options not supported");
Gao Xiangb17500a2018-07-26 20:21:52 +0800292 break;
293 case Opt_nouser_xattr:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800294 erofs_info(sb, "nouser_xattr options not supported");
Gao Xiangb17500a2018-07-26 20:21:52 +0800295 break;
296#endif
297#ifdef CONFIG_EROFS_FS_POSIX_ACL
298 case Opt_acl:
299 set_opt(EROFS_SB(sb), POSIX_ACL);
300 break;
301 case Opt_noacl:
302 clear_opt(EROFS_SB(sb), POSIX_ACL);
303 break;
304#else
305 case Opt_acl:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800306 erofs_info(sb, "acl options not supported");
Gao Xiangb17500a2018-07-26 20:21:52 +0800307 break;
308 case Opt_noacl:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800309 erofs_info(sb, "noacl options not supported");
Gao Xiangb17500a2018-07-26 20:21:52 +0800310 break;
311#endif
Gao Xiang4279f3f2019-07-31 23:57:49 +0800312 case Opt_cache_strategy:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800313 err = erofs_build_cache_strategy(sb, args);
Gao Xiang4279f3f2019-07-31 23:57:49 +0800314 if (err)
315 return err;
316 break;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800317 default:
Gao Xiang4f761fa2019-09-04 10:09:09 +0800318 erofs_err(sb, "Unrecognized mount option \"%s\" or missing value", p);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800319 return -EINVAL;
320 }
321 }
322 return 0;
323}
324
Gao Xiang4279f3f2019-07-31 23:57:49 +0800325#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang105d4ad2018-07-26 20:22:07 +0800326static const struct address_space_operations managed_cache_aops;
327
Gao Xiang99634bf2019-09-04 10:09:05 +0800328static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800329{
330 int ret = 1; /* 0 - busy */
331 struct address_space *const mapping = page->mapping;
332
Gao Xiang8b987bc2018-12-05 21:23:13 +0800333 DBG_BUGON(!PageLocked(page));
334 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800335
336 if (PagePrivate(page))
Gao Xiang47e541a2018-07-29 13:34:58 +0800337 ret = erofs_try_to_free_cached_page(mapping, page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800338
339 return ret;
340}
341
Gao Xiang99634bf2019-09-04 10:09:05 +0800342static void erofs_managed_cache_invalidatepage(struct page *page,
343 unsigned int offset,
344 unsigned int length)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800345{
346 const unsigned int stop = length + offset;
347
Gao Xiang8b987bc2018-12-05 21:23:13 +0800348 DBG_BUGON(!PageLocked(page));
Gao Xiang105d4ad2018-07-26 20:22:07 +0800349
Gao Xiang8b987bc2018-12-05 21:23:13 +0800350 /* Check for potential overflow in debug mode */
351 DBG_BUGON(stop > PAGE_SIZE || stop < length);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800352
353 if (offset == 0 && stop == PAGE_SIZE)
Gao Xiang99634bf2019-09-04 10:09:05 +0800354 while (!erofs_managed_cache_releasepage(page, GFP_NOFS))
Gao Xiang105d4ad2018-07-26 20:22:07 +0800355 cond_resched();
356}
357
358static const struct address_space_operations managed_cache_aops = {
Gao Xiang99634bf2019-09-04 10:09:05 +0800359 .releasepage = erofs_managed_cache_releasepage,
360 .invalidatepage = erofs_managed_cache_invalidatepage,
Gao Xiang105d4ad2018-07-26 20:22:07 +0800361};
362
Gao Xiang8f7acda2019-07-31 23:57:41 +0800363static int erofs_init_managed_cache(struct super_block *sb)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800364{
Gao Xiang8f7acda2019-07-31 23:57:41 +0800365 struct erofs_sb_info *const sbi = EROFS_SB(sb);
366 struct inode *const inode = new_inode(sb);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800367
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800368 if (!inode)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800369 return -ENOMEM;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800370
371 set_nlink(inode, 1);
372 inode->i_size = OFFSET_MAX;
373
374 inode->i_mapping->a_ops = &managed_cache_aops;
375 mapping_set_gfp_mask(inode->i_mapping,
Gao Xiang8494c292019-07-31 23:57:42 +0800376 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800377 sbi->managed_cache = inode;
378 return 0;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800379}
Gao Xiang8f7acda2019-07-31 23:57:41 +0800380#else
381static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800382#endif
383
Gao Xiang9e794de2019-07-31 23:57:40 +0800384static int erofs_fill_super(struct super_block *sb, void *data, int silent)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800385{
386 struct inode *inode;
387 struct erofs_sb_info *sbi;
Gao Xiang8f7acda2019-07-31 23:57:41 +0800388 int err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800389
Gao Xiang8f7acda2019-07-31 23:57:41 +0800390 sb->s_magic = EROFS_SUPER_MAGIC;
391
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800392 if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800393 erofs_err(sb, "failed to set erofs blksize");
Gao Xiang8f7acda2019-07-31 23:57:41 +0800394 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800395 }
396
Shobhit Kukretia9f69bd2019-06-26 22:31:18 -0700397 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800398 if (!sbi)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800399 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800400
Gao Xiang8f7acda2019-07-31 23:57:41 +0800401 sb->s_fs_info = sbi;
Gao Xiang99634bf2019-09-04 10:09:05 +0800402 err = erofs_read_superblock(sb);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800403 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800404 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800405
Gao Xiang5f0abea2018-09-06 17:01:47 +0800406 sb->s_flags |= SB_RDONLY | SB_NOATIME;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800407 sb->s_maxbytes = MAX_LFS_FILESIZE;
408 sb->s_time_gran = 1;
409
410 sb->s_op = &erofs_sops;
Gao Xiangb17500a2018-07-26 20:21:52 +0800411 sb->s_xattr = erofs_xattr_handlers;
Chengguang Xue7cda1e2020-05-26 17:03:43 +0800412
Gao Xiangba2b77a2018-07-26 20:21:46 +0800413 /* set erofs default mount options */
Gao Xiang99634bf2019-09-04 10:09:05 +0800414 erofs_default_options(sbi);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800415
Gao Xiang99634bf2019-09-04 10:09:05 +0800416 err = erofs_parse_options(sb, data);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800417 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800418 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800419
Gao Xiang516c115c2019-01-29 16:35:20 +0800420 if (test_opt(sbi, POSIX_ACL))
421 sb->s_flags |= SB_POSIXACL;
422 else
423 sb->s_flags &= ~SB_POSIXACL;
424
Gao Xiange7e9a302018-07-26 20:22:05 +0800425#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang64094a02020-02-20 10:46:42 +0800426 xa_init(&sbi->managed_pslots);
Gao Xiange7e9a302018-07-26 20:22:05 +0800427#endif
428
Gao Xiangba2b77a2018-07-26 20:21:46 +0800429 /* get the root inode */
430 inode = erofs_iget(sb, ROOT_NID(sbi), true);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800431 if (IS_ERR(inode))
432 return PTR_ERR(inode);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800433
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800434 if (!S_ISDIR(inode->i_mode)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800435 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
436 ROOT_NID(sbi), inode->i_mode);
Chengguang Xu94832d92019-01-23 14:12:25 +0800437 iput(inode);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800438 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800439 }
440
441 sb->s_root = d_make_root(inode);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800442 if (!sb->s_root)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800443 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800444
Gao Xiang22fe04a2019-07-31 23:57:39 +0800445 erofs_shrinker_register(sb);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800446 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
447 err = erofs_init_managed_cache(sb);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800448 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800449 return err;
Gao Xiang2497ee42018-07-26 20:22:03 +0800450
Gao Xiang4f761fa2019-09-04 10:09:09 +0800451 erofs_info(sb, "mounted with opts: %s, root inode @ nid %llu.",
452 (char *)data, ROOT_NID(sbi));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800453 return 0;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800454}
455
Gao Xiang9e794de2019-07-31 23:57:40 +0800456static struct dentry *erofs_mount(struct file_system_type *fs_type, int flags,
457 const char *dev_name, void *data)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800458{
Gao Xiang9e794de2019-07-31 23:57:40 +0800459 return mount_bdev(fs_type, flags, dev_name, data, erofs_fill_super);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800460}
461
Gao Xiang8f7acda2019-07-31 23:57:41 +0800462/*
463 * could be triggered after deactivate_locked_super()
464 * is called, thus including umount and failed to initialize.
465 */
466static void erofs_kill_sb(struct super_block *sb)
467{
468 struct erofs_sb_info *sbi;
469
470 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800471
472 kill_block_super(sb);
473
474 sbi = EROFS_SB(sb);
475 if (!sbi)
476 return;
477 kfree(sbi);
478 sb->s_fs_info = NULL;
479}
480
481/* called when ->s_root is non-NULL */
482static void erofs_put_super(struct super_block *sb)
483{
484 struct erofs_sb_info *const sbi = EROFS_SB(sb);
485
486 DBG_BUGON(!sbi);
487
488 erofs_shrinker_unregister(sb);
Gao Xiang4279f3f2019-07-31 23:57:49 +0800489#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang8f7acda2019-07-31 23:57:41 +0800490 iput(sbi->managed_cache);
491 sbi->managed_cache = NULL;
492#endif
493}
494
Gao Xiangba2b77a2018-07-26 20:21:46 +0800495static struct file_system_type erofs_fs_type = {
496 .owner = THIS_MODULE,
497 .name = "erofs",
498 .mount = erofs_mount,
Gao Xiang8f7acda2019-07-31 23:57:41 +0800499 .kill_sb = erofs_kill_sb,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800500 .fs_flags = FS_REQUIRES_DEV,
501};
502MODULE_ALIAS_FS("erofs");
503
504static int __init erofs_module_init(void)
505{
506 int err;
507
508 erofs_check_ondisk_layout_definitions();
Gao Xiangba2b77a2018-07-26 20:21:46 +0800509
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800510 erofs_inode_cachep = kmem_cache_create("erofs_inode",
Gao Xianga5876e22019-09-04 10:08:56 +0800511 sizeof(struct erofs_inode), 0,
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800512 SLAB_RECLAIM_ACCOUNT,
Gao Xiang99634bf2019-09-04 10:09:05 +0800513 erofs_inode_init_once);
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800514 if (!erofs_inode_cachep) {
515 err = -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800516 goto icache_err;
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800517 }
Gao Xiangba2b77a2018-07-26 20:21:46 +0800518
Gao Xiang22fe04a2019-07-31 23:57:39 +0800519 err = erofs_init_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800520 if (err)
521 goto shrinker_err;
522
Gao Xiang3883a792018-07-26 20:22:06 +0800523 err = z_erofs_init_zip_subsystem();
524 if (err)
525 goto zip_err;
Gao Xiang3883a792018-07-26 20:22:06 +0800526
Gao Xiangba2b77a2018-07-26 20:21:46 +0800527 err = register_filesystem(&erofs_fs_type);
528 if (err)
529 goto fs_err;
530
Gao Xiangba2b77a2018-07-26 20:21:46 +0800531 return 0;
532
533fs_err:
Gao Xiang3883a792018-07-26 20:22:06 +0800534 z_erofs_exit_zip_subsystem();
535zip_err:
Gao Xiang22fe04a2019-07-31 23:57:39 +0800536 erofs_exit_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800537shrinker_err:
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800538 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800539icache_err:
540 return err;
541}
542
543static void __exit erofs_module_exit(void)
544{
545 unregister_filesystem(&erofs_fs_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800546 z_erofs_exit_zip_subsystem();
Gao Xiang22fe04a2019-07-31 23:57:39 +0800547 erofs_exit_shrinker();
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800548
549 /* Ensure all RCU free inodes are safe before cache is destroyed. */
550 rcu_barrier();
551 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800552}
553
554/* get filesystem statistics */
555static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
556{
557 struct super_block *sb = dentry->d_sb;
558 struct erofs_sb_info *sbi = EROFS_SB(sb);
559 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
560
561 buf->f_type = sb->s_magic;
562 buf->f_bsize = EROFS_BLKSIZ;
563 buf->f_blocks = sbi->blocks;
564 buf->f_bfree = buf->f_bavail = 0;
565
566 buf->f_files = ULLONG_MAX;
567 buf->f_ffree = ULLONG_MAX - sbi->inos;
568
569 buf->f_namelen = EROFS_NAME_LEN;
570
571 buf->f_fsid.val[0] = (u32)id;
572 buf->f_fsid.val[1] = (u32)(id >> 32);
573 return 0;
574}
575
576static int erofs_show_options(struct seq_file *seq, struct dentry *root)
577{
Gao Xiangb17500a2018-07-26 20:21:52 +0800578 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
579
580#ifdef CONFIG_EROFS_FS_XATTR
581 if (test_opt(sbi, XATTR_USER))
582 seq_puts(seq, ",user_xattr");
583 else
584 seq_puts(seq, ",nouser_xattr");
585#endif
586#ifdef CONFIG_EROFS_FS_POSIX_ACL
587 if (test_opt(sbi, POSIX_ACL))
588 seq_puts(seq, ",acl");
589 else
590 seq_puts(seq, ",noacl");
591#endif
Gao Xiang4279f3f2019-07-31 23:57:49 +0800592#ifdef CONFIG_EROFS_FS_ZIP
593 if (sbi->cache_strategy == EROFS_ZIP_CACHE_DISABLED) {
594 seq_puts(seq, ",cache_strategy=disabled");
595 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAHEAD) {
596 seq_puts(seq, ",cache_strategy=readahead");
597 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAROUND) {
598 seq_puts(seq, ",cache_strategy=readaround");
Gao Xiang4279f3f2019-07-31 23:57:49 +0800599 }
600#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800601 return 0;
602}
603
604static int erofs_remount(struct super_block *sb, int *flags, char *data)
605{
Chengguang Xud41076e2018-09-19 22:53:46 +0800606 struct erofs_sb_info *sbi = EROFS_SB(sb);
607 unsigned int org_mnt_opt = sbi->mount_opt;
Chengguang Xud41076e2018-09-19 22:53:46 +0800608 int err;
609
Gao Xiang8b987bc2018-12-05 21:23:13 +0800610 DBG_BUGON(!sb_rdonly(sb));
Gao Xiang99634bf2019-09-04 10:09:05 +0800611 err = erofs_parse_options(sb, data);
Chengguang Xud41076e2018-09-19 22:53:46 +0800612 if (err)
613 goto out;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800614
Gao Xiang516c115c2019-01-29 16:35:20 +0800615 if (test_opt(sbi, POSIX_ACL))
616 sb->s_flags |= SB_POSIXACL;
617 else
618 sb->s_flags &= ~SB_POSIXACL;
619
Gao Xiang5f0abea2018-09-06 17:01:47 +0800620 *flags |= SB_RDONLY;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800621 return 0;
Chengguang Xud41076e2018-09-19 22:53:46 +0800622out:
Chengguang Xud41076e2018-09-19 22:53:46 +0800623 sbi->mount_opt = org_mnt_opt;
Chengguang Xud41076e2018-09-19 22:53:46 +0800624 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800625}
626
627const struct super_operations erofs_sops = {
628 .put_super = erofs_put_super,
Gao Xiang99634bf2019-09-04 10:09:05 +0800629 .alloc_inode = erofs_alloc_inode,
630 .free_inode = erofs_free_inode,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800631 .statfs = erofs_statfs,
632 .show_options = erofs_show_options,
633 .remount_fs = erofs_remount,
634};
635
636module_init(erofs_module_init);
637module_exit(erofs_module_exit);
638
639MODULE_DESCRIPTION("Enhanced ROM File System");
Gao Xiangbc33d9f2019-07-31 23:57:51 +0800640MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800641MODULE_LICENSE("GPL");
642