blob: 499dc7f5d0e6e762928c607b6ef9ffd4fc6bdf71 [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>
Gao Xiang6af7b482019-01-14 19:40:25 +080012#include "xattr.h"
Gao Xiangba2b77a2018-07-26 20:21:46 +080013
Chao Yu13f06f42018-07-26 20:21:55 +080014#define CREATE_TRACE_POINTS
15#include <trace/events/erofs.h>
16
Gao Xiangba2b77a2018-07-26 20:21:46 +080017static struct kmem_cache *erofs_inode_cachep __read_mostly;
18
19static void init_once(void *ptr)
20{
21 struct erofs_vnode *vi = ptr;
22
23 inode_init_once(&vi->vfs_inode);
24}
25
Gao Xiangba2b77a2018-07-26 20:21:46 +080026static struct inode *alloc_inode(struct super_block *sb)
27{
28 struct erofs_vnode *vi =
29 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
30
Vatsala Narange2ff9f12019-03-21 05:36:13 +053031 if (!vi)
Gao Xiangba2b77a2018-07-26 20:21:46 +080032 return NULL;
33
34 /* zero out everything except vfs_inode */
35 memset(vi, 0, offsetof(struct erofs_vnode, vfs_inode));
36 return &vi->vfs_inode;
37}
38
Al Viro25af6c42019-04-10 14:59:08 -040039static void free_inode(struct inode *inode)
Gao Xiangba2b77a2018-07-26 20:21:46 +080040{
Gao Xiangba2b77a2018-07-26 20:21:46 +080041 struct erofs_vnode *vi = EROFS_V(inode);
42
43 /* be careful RCU symlink path (see ext4_inode_info->i_data)! */
44 if (is_inode_fast_symlink(inode))
45 kfree(inode->i_link);
46
47 kfree(vi->xattr_shared_xattrs);
48
49 kmem_cache_free(erofs_inode_cachep, vi);
50}
51
Gao Xiang5efe5132019-06-13 16:35:41 +080052static bool check_layout_compatibility(struct super_block *sb,
53 struct erofs_super_block *layout)
54{
Gao Xiang426a9302019-09-04 10:08:53 +080055 const unsigned int feature = le32_to_cpu(layout->feature_incompat);
Gao Xiang5efe5132019-06-13 16:35:41 +080056
Gao Xiang426a9302019-09-04 10:08:53 +080057 EROFS_SB(sb)->feature_incompat = feature;
Gao Xiang5efe5132019-06-13 16:35:41 +080058
59 /* check if current kernel meets all mandatory requirements */
Gao Xiang426a9302019-09-04 10:08:53 +080060 if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
61 errln("unidentified incompatible feature %x, please upgrade kernel version",
62 feature & ~EROFS_ALL_FEATURE_INCOMPAT);
Gao Xiang5efe5132019-06-13 16:35:41 +080063 return false;
64 }
65 return true;
66}
67
Gao Xiangba2b77a2018-07-26 20:21:46 +080068static int superblock_read(struct super_block *sb)
69{
70 struct erofs_sb_info *sbi;
71 struct buffer_head *bh;
72 struct erofs_super_block *layout;
Thomas Weißschuh7dd68b12018-09-10 21:41:14 +020073 unsigned int blkszbits;
Gao Xiangba2b77a2018-07-26 20:21:46 +080074 int ret;
75
76 bh = sb_bread(sb, 0);
77
Vatsala Narange2ff9f12019-03-21 05:36:13 +053078 if (!bh) {
Gao Xiangba2b77a2018-07-26 20:21:46 +080079 errln("cannot read erofs superblock");
80 return -EIO;
81 }
82
83 sbi = EROFS_SB(sb);
84 layout = (struct erofs_super_block *)((u8 *)bh->b_data
85 + EROFS_SUPER_OFFSET);
86
87 ret = -EINVAL;
88 if (le32_to_cpu(layout->magic) != EROFS_SUPER_MAGIC_V1) {
89 errln("cannot find valid erofs superblock");
90 goto out;
91 }
92
93 blkszbits = layout->blkszbits;
94 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
Gao Xiang8d8a09b2019-08-30 00:38:27 +080095 if (blkszbits != LOG_BLOCK_SIZE) {
Gao Xiangba2b77a2018-07-26 20:21:46 +080096 errln("blksize %u isn't supported on this platform",
Julian Merida447a3622019-03-18 20:58:41 -030097 1 << blkszbits);
Gao Xiangba2b77a2018-07-26 20:21:46 +080098 goto out;
99 }
100
Gao Xiang5efe5132019-06-13 16:35:41 +0800101 if (!check_layout_compatibility(sb, layout))
102 goto out;
103
Gao Xiangba2b77a2018-07-26 20:21:46 +0800104 sbi->blocks = le32_to_cpu(layout->blocks);
105 sbi->meta_blkaddr = le32_to_cpu(layout->meta_blkaddr);
Gao Xiangb17500a2018-07-26 20:21:52 +0800106#ifdef CONFIG_EROFS_FS_XATTR
107 sbi->xattr_blkaddr = le32_to_cpu(layout->xattr_blkaddr);
108#endif
Gao Xiang8a765682019-09-04 10:08:54 +0800109 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800110 sbi->root_nid = le16_to_cpu(layout->root_nid);
111 sbi->inos = le64_to_cpu(layout->inos);
112
113 sbi->build_time = le64_to_cpu(layout->build_time);
114 sbi->build_time_nsec = le32_to_cpu(layout->build_time_nsec);
115
116 memcpy(&sb->s_uuid, layout->uuid, sizeof(layout->uuid));
Gao Xiangba2b77a2018-07-26 20:21:46 +0800117
Gao Xianga64d9492019-08-18 18:28:24 +0800118 ret = strscpy(sbi->volume_name, layout->volume_name,
119 sizeof(layout->volume_name));
120 if (ret < 0) { /* -E2BIG */
121 errln("bad volume name without NIL terminator");
122 ret = -EFSCORRUPTED;
123 goto out;
124 }
Gao Xiangba2b77a2018-07-26 20:21:46 +0800125 ret = 0;
126out:
127 brelse(bh);
128 return ret;
129}
130
Chao Yu9c07b3b2018-07-26 20:21:54 +0800131#ifdef CONFIG_EROFS_FAULT_INJECTION
Gao Xiang14a56ec2019-03-25 11:40:09 +0800132const char *erofs_fault_name[FAULT_MAX] = {
Chao Yu9c07b3b2018-07-26 20:21:54 +0800133 [FAULT_KMALLOC] = "kmalloc",
Gao Xiang14a56ec2019-03-25 11:40:09 +0800134 [FAULT_READ_IO] = "read IO error",
Chao Yu9c07b3b2018-07-26 20:21:54 +0800135};
136
Chengguang Xud41076e2018-09-19 22:53:46 +0800137static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
138 unsigned int rate)
Chao Yu9c07b3b2018-07-26 20:21:54 +0800139{
140 struct erofs_fault_info *ffi = &sbi->fault_info;
141
142 if (rate) {
143 atomic_set(&ffi->inject_ops, 0);
144 ffi->inject_rate = rate;
145 ffi->inject_type = (1 << FAULT_MAX) - 1;
146 } else {
147 memset(ffi, 0, sizeof(struct erofs_fault_info));
148 }
Chengguang Xu01e4ae42018-09-19 22:53:44 +0800149
150 set_opt(sbi, FAULT_INJECTION);
Chengguang Xud41076e2018-09-19 22:53:46 +0800151}
152
153static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
154 substring_t *args)
155{
156 int rate = 0;
157
158 if (args->from && match_int(args, &rate))
159 return -EINVAL;
160
161 __erofs_build_fault_attr(sbi, rate);
Chengguang Xu01e4ae42018-09-19 22:53:44 +0800162 return 0;
163}
Chengguang Xu2ab3dd82018-09-19 22:53:45 +0800164
165static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
166{
167 return sbi->fault_info.inject_rate;
168}
Chengguang Xu01e4ae42018-09-19 22:53:44 +0800169#else
Chengguang Xud41076e2018-09-19 22:53:46 +0800170static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
171 unsigned int rate)
172{
173}
174
Chengguang Xu01e4ae42018-09-19 22:53:44 +0800175static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
176 substring_t *args)
177{
178 infoln("fault_injection options not supported");
179 return 0;
Chao Yu9c07b3b2018-07-26 20:21:54 +0800180}
Chengguang Xu2ab3dd82018-09-19 22:53:45 +0800181
182static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
183{
184 return 0;
185}
Chao Yu9c07b3b2018-07-26 20:21:54 +0800186#endif
187
Gao Xiang5fb76bb2018-09-20 00:06:56 +0800188#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang4279f3f2019-07-31 23:57:49 +0800189static int erofs_build_cache_strategy(struct erofs_sb_info *sbi,
190 substring_t *args)
191{
192 const char *cs = match_strdup(args);
193 int err = 0;
194
195 if (!cs) {
196 errln("Not enough memory to store cache strategy");
197 return -ENOMEM;
198 }
199
200 if (!strcmp(cs, "disabled")) {
201 sbi->cache_strategy = EROFS_ZIP_CACHE_DISABLED;
202 } else if (!strcmp(cs, "readahead")) {
203 sbi->cache_strategy = EROFS_ZIP_CACHE_READAHEAD;
204 } else if (!strcmp(cs, "readaround")) {
205 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
206 } else {
207 errln("Unrecognized cache strategy \"%s\"", cs);
208 err = -EINVAL;
209 }
210 kfree(cs);
211 return err;
212}
213#else
214static int erofs_build_cache_strategy(struct erofs_sb_info *sbi,
215 substring_t *args)
216{
217 infoln("EROFS compression is disabled, so cache strategy is ignored");
218 return 0;
219}
Gao Xiang5fb76bb2018-09-20 00:06:56 +0800220#endif
221
Gao Xiang4279f3f2019-07-31 23:57:49 +0800222/* set up default EROFS parameters */
223static void default_options(struct erofs_sb_info *sbi)
224{
225#ifdef CONFIG_EROFS_FS_ZIP
226 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
227 sbi->max_sync_decompress_pages = 3;
228#endif
Gao Xiangb17500a2018-07-26 20:21:52 +0800229#ifdef CONFIG_EROFS_FS_XATTR
230 set_opt(sbi, XATTR_USER);
231#endif
Gao Xiangb17500a2018-07-26 20:21:52 +0800232#ifdef CONFIG_EROFS_FS_POSIX_ACL
233 set_opt(sbi, POSIX_ACL);
234#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800235}
236
237enum {
Gao Xiangb17500a2018-07-26 20:21:52 +0800238 Opt_user_xattr,
239 Opt_nouser_xattr,
240 Opt_acl,
241 Opt_noacl,
Chao Yu9c07b3b2018-07-26 20:21:54 +0800242 Opt_fault_injection,
Gao Xiang4279f3f2019-07-31 23:57:49 +0800243 Opt_cache_strategy,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800244 Opt_err
245};
246
247static match_table_t erofs_tokens = {
Gao Xiangb17500a2018-07-26 20:21:52 +0800248 {Opt_user_xattr, "user_xattr"},
249 {Opt_nouser_xattr, "nouser_xattr"},
250 {Opt_acl, "acl"},
251 {Opt_noacl, "noacl"},
Chao Yu9c07b3b2018-07-26 20:21:54 +0800252 {Opt_fault_injection, "fault_injection=%u"},
Gao Xiang4279f3f2019-07-31 23:57:49 +0800253 {Opt_cache_strategy, "cache_strategy=%s"},
Gao Xiangba2b77a2018-07-26 20:21:46 +0800254 {Opt_err, NULL}
255};
256
257static int parse_options(struct super_block *sb, char *options)
258{
259 substring_t args[MAX_OPT_ARGS];
260 char *p;
Chengguang Xu01e4ae42018-09-19 22:53:44 +0800261 int err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800262
263 if (!options)
264 return 0;
265
Bhanusree Pola561fb352019-03-22 10:38:16 +0800266 while ((p = strsep(&options, ","))) {
Gao Xiangba2b77a2018-07-26 20:21:46 +0800267 int token;
268
269 if (!*p)
270 continue;
271
272 args[0].to = args[0].from = NULL;
273 token = match_token(p, erofs_tokens, args);
274
275 switch (token) {
Gao Xiangb17500a2018-07-26 20:21:52 +0800276#ifdef CONFIG_EROFS_FS_XATTR
277 case Opt_user_xattr:
278 set_opt(EROFS_SB(sb), XATTR_USER);
279 break;
280 case Opt_nouser_xattr:
281 clear_opt(EROFS_SB(sb), XATTR_USER);
282 break;
283#else
284 case Opt_user_xattr:
285 infoln("user_xattr options not supported");
286 break;
287 case Opt_nouser_xattr:
288 infoln("nouser_xattr options not supported");
289 break;
290#endif
291#ifdef CONFIG_EROFS_FS_POSIX_ACL
292 case Opt_acl:
293 set_opt(EROFS_SB(sb), POSIX_ACL);
294 break;
295 case Opt_noacl:
296 clear_opt(EROFS_SB(sb), POSIX_ACL);
297 break;
298#else
299 case Opt_acl:
300 infoln("acl options not supported");
301 break;
302 case Opt_noacl:
303 infoln("noacl options not supported");
304 break;
305#endif
Chao Yu9c07b3b2018-07-26 20:21:54 +0800306 case Opt_fault_injection:
Chengguang Xu01e4ae42018-09-19 22:53:44 +0800307 err = erofs_build_fault_attr(EROFS_SB(sb), args);
308 if (err)
309 return err;
Chao Yu9c07b3b2018-07-26 20:21:54 +0800310 break;
Gao Xiang4279f3f2019-07-31 23:57:49 +0800311 case Opt_cache_strategy:
312 err = erofs_build_cache_strategy(EROFS_SB(sb), args);
313 if (err)
314 return err;
315 break;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800316 default:
Gao Xiangbc33d9f2019-07-31 23:57:51 +0800317 errln("Unrecognized mount option \"%s\" or missing value", p);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800318 return -EINVAL;
319 }
320 }
321 return 0;
322}
323
Gao Xiang4279f3f2019-07-31 23:57:49 +0800324#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang105d4ad2018-07-26 20:22:07 +0800325static const struct address_space_operations managed_cache_aops;
326
327static int managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
328{
329 int ret = 1; /* 0 - busy */
330 struct address_space *const mapping = page->mapping;
331
Gao Xiang8b987bc2018-12-05 21:23:13 +0800332 DBG_BUGON(!PageLocked(page));
333 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800334
335 if (PagePrivate(page))
Gao Xiang47e541a2018-07-29 13:34:58 +0800336 ret = erofs_try_to_free_cached_page(mapping, page);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800337
338 return ret;
339}
340
341static void managed_cache_invalidatepage(struct page *page,
Julian Merida447a3622019-03-18 20:58:41 -0300342 unsigned int offset,
343 unsigned int length)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800344{
345 const unsigned int stop = length + offset;
346
Gao Xiang8b987bc2018-12-05 21:23:13 +0800347 DBG_BUGON(!PageLocked(page));
Gao Xiang105d4ad2018-07-26 20:22:07 +0800348
Gao Xiang8b987bc2018-12-05 21:23:13 +0800349 /* Check for potential overflow in debug mode */
350 DBG_BUGON(stop > PAGE_SIZE || stop < length);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800351
352 if (offset == 0 && stop == PAGE_SIZE)
353 while (!managed_cache_releasepage(page, GFP_NOFS))
354 cond_resched();
355}
356
357static const struct address_space_operations managed_cache_aops = {
358 .releasepage = managed_cache_releasepage,
359 .invalidatepage = managed_cache_invalidatepage,
360};
361
Gao Xiang8f7acda2019-07-31 23:57:41 +0800362static int erofs_init_managed_cache(struct super_block *sb)
Gao Xiang105d4ad2018-07-26 20:22:07 +0800363{
Gao Xiang8f7acda2019-07-31 23:57:41 +0800364 struct erofs_sb_info *const sbi = EROFS_SB(sb);
365 struct inode *const inode = new_inode(sb);
Gao Xiang105d4ad2018-07-26 20:22:07 +0800366
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800367 if (!inode)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800368 return -ENOMEM;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800369
370 set_nlink(inode, 1);
371 inode->i_size = OFFSET_MAX;
372
373 inode->i_mapping->a_ops = &managed_cache_aops;
374 mapping_set_gfp_mask(inode->i_mapping,
Gao Xiang8494c292019-07-31 23:57:42 +0800375 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800376 sbi->managed_cache = inode;
377 return 0;
Gao Xiang105d4ad2018-07-26 20:22:07 +0800378}
Gao Xiang8f7acda2019-07-31 23:57:41 +0800379#else
380static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
Gao Xiang105d4ad2018-07-26 20:22:07 +0800381#endif
382
Gao Xiang9e794de2019-07-31 23:57:40 +0800383static int erofs_fill_super(struct super_block *sb, void *data, int silent)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800384{
385 struct inode *inode;
386 struct erofs_sb_info *sbi;
Gao Xiang8f7acda2019-07-31 23:57:41 +0800387 int err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800388
Gao Xiang9e794de2019-07-31 23:57:40 +0800389 infoln("fill_super, device -> %s", sb->s_id);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800390 infoln("options -> %s", (char *)data);
391
Gao Xiang8f7acda2019-07-31 23:57:41 +0800392 sb->s_magic = EROFS_SUPER_MAGIC;
393
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800394 if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
Gao Xiangba2b77a2018-07-26 20:21:46 +0800395 errln("failed to set erofs blksize");
Gao Xiang8f7acda2019-07-31 23:57:41 +0800396 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800397 }
398
Shobhit Kukretia9f69bd2019-06-26 22:31:18 -0700399 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800400 if (!sbi)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800401 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800402
Gao Xiang8f7acda2019-07-31 23:57:41 +0800403 sb->s_fs_info = sbi;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800404 err = superblock_read(sb);
405 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800406 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800407
Gao Xiang5f0abea2018-09-06 17:01:47 +0800408 sb->s_flags |= SB_RDONLY | SB_NOATIME;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800409 sb->s_maxbytes = MAX_LFS_FILESIZE;
410 sb->s_time_gran = 1;
411
412 sb->s_op = &erofs_sops;
413
Gao Xiangb17500a2018-07-26 20:21:52 +0800414#ifdef CONFIG_EROFS_FS_XATTR
415 sb->s_xattr = erofs_xattr_handlers;
416#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800417 /* set erofs default mount options */
418 default_options(sbi);
419
420 err = parse_options(sb, data);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800421 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800422 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800423
424 if (!silent)
425 infoln("root inode @ nid %llu", ROOT_NID(sbi));
426
Gao Xiang516c115c2019-01-29 16:35:20 +0800427 if (test_opt(sbi, POSIX_ACL))
428 sb->s_flags |= SB_POSIXACL;
429 else
430 sb->s_flags &= ~SB_POSIXACL;
431
Gao Xiange7e9a302018-07-26 20:22:05 +0800432#ifdef CONFIG_EROFS_FS_ZIP
433 INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
434#endif
435
Gao Xiangba2b77a2018-07-26 20:21:46 +0800436 /* get the root inode */
437 inode = erofs_iget(sb, ROOT_NID(sbi), true);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800438 if (IS_ERR(inode))
439 return PTR_ERR(inode);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800440
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800441 if (!S_ISDIR(inode->i_mode)) {
Gao Xiangba2b77a2018-07-26 20:21:46 +0800442 errln("rootino(nid %llu) is not a directory(i_mode %o)",
Julian Merida447a3622019-03-18 20:58:41 -0300443 ROOT_NID(sbi), inode->i_mode);
Chengguang Xu94832d92019-01-23 14:12:25 +0800444 iput(inode);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800445 return -EINVAL;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800446 }
447
448 sb->s_root = d_make_root(inode);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800449 if (!sb->s_root)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800450 return -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800451
Gao Xiang22fe04a2019-07-31 23:57:39 +0800452 erofs_shrinker_register(sb);
Gao Xiang8f7acda2019-07-31 23:57:41 +0800453 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
454 err = erofs_init_managed_cache(sb);
Gao Xiang8d8a09b2019-08-30 00:38:27 +0800455 if (err)
Gao Xiang8f7acda2019-07-31 23:57:41 +0800456 return err;
Gao Xiang2497ee42018-07-26 20:22:03 +0800457
Gao Xiangba2b77a2018-07-26 20:21:46 +0800458 if (!silent)
Gao Xiang9e794de2019-07-31 23:57:40 +0800459 infoln("mounted on %s with opts: %s.", sb->s_id, (char *)data);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800460 return 0;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800461}
462
Gao Xiang9e794de2019-07-31 23:57:40 +0800463static struct dentry *erofs_mount(struct file_system_type *fs_type, int flags,
464 const char *dev_name, void *data)
Gao Xiangba2b77a2018-07-26 20:21:46 +0800465{
Gao Xiang9e794de2019-07-31 23:57:40 +0800466 return mount_bdev(fs_type, flags, dev_name, data, erofs_fill_super);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800467}
468
Gao Xiang8f7acda2019-07-31 23:57:41 +0800469/*
470 * could be triggered after deactivate_locked_super()
471 * is called, thus including umount and failed to initialize.
472 */
473static void erofs_kill_sb(struct super_block *sb)
474{
475 struct erofs_sb_info *sbi;
476
477 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
478 infoln("unmounting for %s", sb->s_id);
479
480 kill_block_super(sb);
481
482 sbi = EROFS_SB(sb);
483 if (!sbi)
484 return;
485 kfree(sbi);
486 sb->s_fs_info = NULL;
487}
488
489/* called when ->s_root is non-NULL */
490static void erofs_put_super(struct super_block *sb)
491{
492 struct erofs_sb_info *const sbi = EROFS_SB(sb);
493
494 DBG_BUGON(!sbi);
495
496 erofs_shrinker_unregister(sb);
Gao Xiang4279f3f2019-07-31 23:57:49 +0800497#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang8f7acda2019-07-31 23:57:41 +0800498 iput(sbi->managed_cache);
499 sbi->managed_cache = NULL;
500#endif
501}
502
Gao Xiangba2b77a2018-07-26 20:21:46 +0800503static struct file_system_type erofs_fs_type = {
504 .owner = THIS_MODULE,
505 .name = "erofs",
506 .mount = erofs_mount,
Gao Xiang8f7acda2019-07-31 23:57:41 +0800507 .kill_sb = erofs_kill_sb,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800508 .fs_flags = FS_REQUIRES_DEV,
509};
510MODULE_ALIAS_FS("erofs");
511
512static int __init erofs_module_init(void)
513{
514 int err;
515
516 erofs_check_ondisk_layout_definitions();
517 infoln("initializing erofs " EROFS_VERSION);
518
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800519 erofs_inode_cachep = kmem_cache_create("erofs_inode",
520 sizeof(struct erofs_vnode), 0,
521 SLAB_RECLAIM_ACCOUNT,
522 init_once);
523 if (!erofs_inode_cachep) {
524 err = -ENOMEM;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800525 goto icache_err;
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800526 }
Gao Xiangba2b77a2018-07-26 20:21:46 +0800527
Gao Xiang22fe04a2019-07-31 23:57:39 +0800528 err = erofs_init_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800529 if (err)
530 goto shrinker_err;
531
Gao Xiang3883a792018-07-26 20:22:06 +0800532 err = z_erofs_init_zip_subsystem();
533 if (err)
534 goto zip_err;
Gao Xiang3883a792018-07-26 20:22:06 +0800535
Gao Xiangba2b77a2018-07-26 20:21:46 +0800536 err = register_filesystem(&erofs_fs_type);
537 if (err)
538 goto fs_err;
539
540 infoln("successfully to initialize erofs");
541 return 0;
542
543fs_err:
Gao Xiang3883a792018-07-26 20:22:06 +0800544 z_erofs_exit_zip_subsystem();
545zip_err:
Gao Xiang22fe04a2019-07-31 23:57:39 +0800546 erofs_exit_shrinker();
Gao Xianga1581312018-07-26 20:22:04 +0800547shrinker_err:
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800548 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800549icache_err:
550 return err;
551}
552
553static void __exit erofs_module_exit(void)
554{
555 unregister_filesystem(&erofs_fs_type);
Gao Xiang3883a792018-07-26 20:22:06 +0800556 z_erofs_exit_zip_subsystem();
Gao Xiang22fe04a2019-07-31 23:57:39 +0800557 erofs_exit_shrinker();
Gao Xiang1c2dfbf2019-09-04 10:08:55 +0800558
559 /* Ensure all RCU free inodes are safe before cache is destroyed. */
560 rcu_barrier();
561 kmem_cache_destroy(erofs_inode_cachep);
Gao Xiangba2b77a2018-07-26 20:21:46 +0800562 infoln("successfully finalize erofs");
563}
564
565/* get filesystem statistics */
566static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
567{
568 struct super_block *sb = dentry->d_sb;
569 struct erofs_sb_info *sbi = EROFS_SB(sb);
570 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
571
572 buf->f_type = sb->s_magic;
573 buf->f_bsize = EROFS_BLKSIZ;
574 buf->f_blocks = sbi->blocks;
575 buf->f_bfree = buf->f_bavail = 0;
576
577 buf->f_files = ULLONG_MAX;
578 buf->f_ffree = ULLONG_MAX - sbi->inos;
579
580 buf->f_namelen = EROFS_NAME_LEN;
581
582 buf->f_fsid.val[0] = (u32)id;
583 buf->f_fsid.val[1] = (u32)(id >> 32);
584 return 0;
585}
586
587static int erofs_show_options(struct seq_file *seq, struct dentry *root)
588{
Gao Xiangb17500a2018-07-26 20:21:52 +0800589 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
590
591#ifdef CONFIG_EROFS_FS_XATTR
592 if (test_opt(sbi, XATTR_USER))
593 seq_puts(seq, ",user_xattr");
594 else
595 seq_puts(seq, ",nouser_xattr");
596#endif
597#ifdef CONFIG_EROFS_FS_POSIX_ACL
598 if (test_opt(sbi, POSIX_ACL))
599 seq_puts(seq, ",acl");
600 else
601 seq_puts(seq, ",noacl");
602#endif
Chao Yu9c07b3b2018-07-26 20:21:54 +0800603 if (test_opt(sbi, FAULT_INJECTION))
604 seq_printf(seq, ",fault_injection=%u",
Julian Merida447a3622019-03-18 20:58:41 -0300605 erofs_get_fault_rate(sbi));
Gao Xiang4279f3f2019-07-31 23:57:49 +0800606#ifdef CONFIG_EROFS_FS_ZIP
607 if (sbi->cache_strategy == EROFS_ZIP_CACHE_DISABLED) {
608 seq_puts(seq, ",cache_strategy=disabled");
609 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAHEAD) {
610 seq_puts(seq, ",cache_strategy=readahead");
611 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAROUND) {
612 seq_puts(seq, ",cache_strategy=readaround");
613 } else {
614 seq_puts(seq, ",cache_strategy=(unknown)");
615 DBG_BUGON(1);
616 }
617#endif
Gao Xiangba2b77a2018-07-26 20:21:46 +0800618 return 0;
619}
620
621static int erofs_remount(struct super_block *sb, int *flags, char *data)
622{
Chengguang Xud41076e2018-09-19 22:53:46 +0800623 struct erofs_sb_info *sbi = EROFS_SB(sb);
624 unsigned int org_mnt_opt = sbi->mount_opt;
625 unsigned int org_inject_rate = erofs_get_fault_rate(sbi);
626 int err;
627
Gao Xiang8b987bc2018-12-05 21:23:13 +0800628 DBG_BUGON(!sb_rdonly(sb));
Chengguang Xud41076e2018-09-19 22:53:46 +0800629 err = parse_options(sb, data);
630 if (err)
631 goto out;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800632
Gao Xiang516c115c2019-01-29 16:35:20 +0800633 if (test_opt(sbi, POSIX_ACL))
634 sb->s_flags |= SB_POSIXACL;
635 else
636 sb->s_flags &= ~SB_POSIXACL;
637
Gao Xiang5f0abea2018-09-06 17:01:47 +0800638 *flags |= SB_RDONLY;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800639 return 0;
Chengguang Xud41076e2018-09-19 22:53:46 +0800640out:
641 __erofs_build_fault_attr(sbi, org_inject_rate);
642 sbi->mount_opt = org_mnt_opt;
643
644 return err;
Gao Xiangba2b77a2018-07-26 20:21:46 +0800645}
646
647const struct super_operations erofs_sops = {
648 .put_super = erofs_put_super,
649 .alloc_inode = alloc_inode,
Al Viro25af6c42019-04-10 14:59:08 -0400650 .free_inode = free_inode,
Gao Xiangba2b77a2018-07-26 20:21:46 +0800651 .statfs = erofs_statfs,
652 .show_options = erofs_show_options,
653 .remount_fs = erofs_remount,
654};
655
656module_init(erofs_module_init);
657module_exit(erofs_module_exit);
658
659MODULE_DESCRIPTION("Enhanced ROM File System");
Gao Xiangbc33d9f2019-07-31 23:57:51 +0800660MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
Gao Xiangba2b77a2018-07-26 20:21:46 +0800661MODULE_LICENSE("GPL");
662