blob: 057e6d7b5b7f6e50846c292bcf88014548d96d04 [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 Xianga2c75c812019-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 Xu01e4ae4b2018-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 Xiang5f0abea62018-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;
411
Gao Xiangb17500a2018-07-26 20:21:52 +0800412#ifdef CONFIG_EROFS_FS_XATTR
413 sb->s_xattr = erofs_xattr_handlers;
414#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800415 /* set erofs default mount options */
Gao Xiang99634bf2019-09-04 10:09:05 +0800416 erofs_default_options(sbi);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800417
Gao Xiang99634bf2019-09-04 10:09:05 +0800418 err = erofs_parse_options(sb, data);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800419 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800420 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800421
Gao Xiang516c115c2019-01-29 16:35:20 +0800422 if (test_opt(sbi, POSIX_ACL))
423 sb->s_flags |= SB_POSIXACL;
424 else
425 sb->s_flags &= ~SB_POSIXACL;
426
Gao Xiange7e9a302018-07-26 20:22:05 +0800427#ifdef CONFIG_EROFS_FS_ZIP
428 INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
429#endif
430
Gao Xiangba2b77a2018-07-26 20:21:46 +0800431 /* get the root inode */
432 inode = erofs_iget(sb, ROOT_NID(sbi), true);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800433 if (IS_ERR(inode))
434 return PTR_ERR(inode);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800435
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800436 if (!S_ISDIR(inode->i_mode)) {
Gao Xiang4f761fa2019-09-04 10:09:09 +0800437 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
438 ROOT_NID(sbi), inode->i_mode);
Chengguang Xu94832d92019-01-23 14:12:25 +0800439 iput(inode);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800440 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800441 }
442
443 sb->s_root = d_make_root(inode);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800444 if (!sb->s_root)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800445 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800446
Gao Xiang22fe04a2019-07-31 23:57:39 +0800447 erofs_shrinker_register(sb);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800448 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
449 err = erofs_init_managed_cache(sb);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800450 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800451 return err;
Gao Xiang2497ee42018-07-26 20:22:03 +0800452
Gao Xiang4f761fa2019-09-04 10:09:09 +0800453 erofs_info(sb, "mounted with opts: %s, root inode @ nid %llu.",
454 (char *)data, ROOT_NID(sbi));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800455 return 0;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800456}
457
Gao Xiang9e794de2019-07-31 23:57:40 +0800458static struct dentry *erofs_mount(struct file_system_type *fs_type, int flags,
459 const char *dev_name, void *data)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800460{
Gao Xiang9e794de2019-07-31 23:57:40 +0800461 return mount_bdev(fs_type, flags, dev_name, data, erofs_fill_super);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800462}
463
Gao Xiang8f7acda2019-07-31 23:57:41 +0800464/*
465 * could be triggered after deactivate_locked_super()
466 * is called, thus including umount and failed to initialize.
467 */
468static void erofs_kill_sb(struct super_block *sb)
469{
470 struct erofs_sb_info *sbi;
471
472 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800473
474 kill_block_super(sb);
475
476 sbi = EROFS_SB(sb);
477 if (!sbi)
478 return;
479 kfree(sbi);
480 sb->s_fs_info = NULL;
481}
482
483/* called when ->s_root is non-NULL */
484static void erofs_put_super(struct super_block *sb)
485{
486 struct erofs_sb_info *const sbi = EROFS_SB(sb);
487
488 DBG_BUGON(!sbi);
489
490 erofs_shrinker_unregister(sb);
Gao Xiang4279f3f2019-07-31 23:57:49 +0800491#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang8f7acda2019-07-31 23:57:41 +0800492 iput(sbi->managed_cache);
493 sbi->managed_cache = NULL;
494#endif
495}
496
Gao Xiangba2b77a2018-07-26 20:21:46 +0800497static struct file_system_type erofs_fs_type = {
498 .owner = THIS_MODULE,
499 .name = "erofs",
500 .mount = erofs_mount,
Gao Xiang8f7acda2019-07-31 23:57:41 +0800501 .kill_sb = erofs_kill_sb,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800502 .fs_flags = FS_REQUIRES_DEV,
503};
504MODULE_ALIAS_FS("erofs");
505
506static int __init erofs_module_init(void)
507{
508 int err;
509
510 erofs_check_ondisk_layout_definitions();
Gao Xiangba2b77a2018-07-26 20:21:46 +0800511
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800512 erofs_inode_cachep = kmem_cache_create("erofs_inode",
Gao Xianga5876e22019-09-04 10:08:56 +0800513 sizeof(struct erofs_inode), 0,
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800514 SLAB_RECLAIM_ACCOUNT,
Gao Xiang99634bf2019-09-04 10:09:05 +0800515 erofs_inode_init_once);
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800516 if (!erofs_inode_cachep) {
517 err = -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800518 goto icache_err;
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800519 }
Gao Xiangba2b77a2018-07-26 20:21:46 +0800520
Gao Xiang22fe04a2019-07-31 23:57:39 +0800521 err = erofs_init_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800522 if (err)
523 goto shrinker_err;
524
Gao Xiang3883a792018-07-26 20:22:06 +0800525 err = z_erofs_init_zip_subsystem();
526 if (err)
527 goto zip_err;
Gao Xiang3883a792018-07-26 20:22:06 +0800528
Gao Xiangba2b77a2018-07-26 20:21:46 +0800529 err = register_filesystem(&erofs_fs_type);
530 if (err)
531 goto fs_err;
532
Gao Xiangba2b77a2018-07-26 20:21:46 +0800533 return 0;
534
535fs_err:
Gao Xiang3883a792018-07-26 20:22:06 +0800536 z_erofs_exit_zip_subsystem();
537zip_err:
Gao Xiang22fe04a2019-07-31 23:57:39 +0800538 erofs_exit_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800539shrinker_err:
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800540 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800541icache_err:
542 return err;
543}
544
545static void __exit erofs_module_exit(void)
546{
547 unregister_filesystem(&erofs_fs_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800548 z_erofs_exit_zip_subsystem();
Gao Xiang22fe04a2019-07-31 23:57:39 +0800549 erofs_exit_shrinker();
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800550
551 /* Ensure all RCU free inodes are safe before cache is destroyed. */
552 rcu_barrier();
553 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800554}
555
556/* get filesystem statistics */
557static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
558{
559 struct super_block *sb = dentry->d_sb;
560 struct erofs_sb_info *sbi = EROFS_SB(sb);
561 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
562
563 buf->f_type = sb->s_magic;
564 buf->f_bsize = EROFS_BLKSIZ;
565 buf->f_blocks = sbi->blocks;
566 buf->f_bfree = buf->f_bavail = 0;
567
568 buf->f_files = ULLONG_MAX;
569 buf->f_ffree = ULLONG_MAX - sbi->inos;
570
571 buf->f_namelen = EROFS_NAME_LEN;
572
573 buf->f_fsid.val[0] = (u32)id;
574 buf->f_fsid.val[1] = (u32)(id >> 32);
575 return 0;
576}
577
578static int erofs_show_options(struct seq_file *seq, struct dentry *root)
579{
Gao Xiangb17500a2018-07-26 20:21:52 +0800580 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
581
582#ifdef CONFIG_EROFS_FS_XATTR
583 if (test_opt(sbi, XATTR_USER))
584 seq_puts(seq, ",user_xattr");
585 else
586 seq_puts(seq, ",nouser_xattr");
587#endif
588#ifdef CONFIG_EROFS_FS_POSIX_ACL
589 if (test_opt(sbi, POSIX_ACL))
590 seq_puts(seq, ",acl");
591 else
592 seq_puts(seq, ",noacl");
593#endif
Gao Xiang4279f3f2019-07-31 23:57:49 +0800594#ifdef CONFIG_EROFS_FS_ZIP
595 if (sbi->cache_strategy == EROFS_ZIP_CACHE_DISABLED) {
596 seq_puts(seq, ",cache_strategy=disabled");
597 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAHEAD) {
598 seq_puts(seq, ",cache_strategy=readahead");
599 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAROUND) {
600 seq_puts(seq, ",cache_strategy=readaround");
Gao Xiang4279f3f2019-07-31 23:57:49 +0800601 }
602#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800603 return 0;
604}
605
606static int erofs_remount(struct super_block *sb, int *flags, char *data)
607{
Chengguang Xud41076ea2018-09-19 22:53:46 +0800608 struct erofs_sb_info *sbi = EROFS_SB(sb);
609 unsigned int org_mnt_opt = sbi->mount_opt;
Chengguang Xud41076ea2018-09-19 22:53:46 +0800610 int err;
611
Gao Xiang8b987bc2018-12-05 21:23:13 +0800612 DBG_BUGON(!sb_rdonly(sb));
Gao Xiang99634bf2019-09-04 10:09:05 +0800613 err = erofs_parse_options(sb, data);
Chengguang Xud41076ea2018-09-19 22:53:46 +0800614 if (err)
615 goto out;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800616
Gao Xiang516c115c2019-01-29 16:35:20 +0800617 if (test_opt(sbi, POSIX_ACL))
618 sb->s_flags |= SB_POSIXACL;
619 else
620 sb->s_flags &= ~SB_POSIXACL;
621
Gao Xiang5f0abea62018-09-06 17:01:47 +0800622 *flags |= SB_RDONLY;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800623 return 0;
Chengguang Xud41076ea2018-09-19 22:53:46 +0800624out:
Chengguang Xud41076ea2018-09-19 22:53:46 +0800625 sbi->mount_opt = org_mnt_opt;
Chengguang Xud41076ea2018-09-19 22:53:46 +0800626 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800627}
628
629const struct super_operations erofs_sops = {
630 .put_super = erofs_put_super,
Gao Xiang99634bf2019-09-04 10:09:05 +0800631 .alloc_inode = erofs_alloc_inode,
632 .free_inode = erofs_free_inode,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800633 .statfs = erofs_statfs,
634 .show_options = erofs_show_options,
635 .remount_fs = erofs_remount,
636};
637
638module_init(erofs_module_init);
639module_exit(erofs_module_exit);
640
641MODULE_DESCRIPTION("Enhanced ROM File System");
Gao Xiangbc33d9f2019-07-31 23:57:51 +0800642MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800643MODULE_LICENSE("GPL");
644