erofs: save one level of indentation
[linux-block.git] / fs / erofs / super.c
CommitLineData
29b24f6c 1// SPDX-License-Identifier: GPL-2.0-only
ba2b77a8 2/*
ba2b77a8
GX
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * http://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
ba2b77a8
GX
6 */
7#include <linux/module.h>
8#include <linux/buffer_head.h>
9#include <linux/statfs.h>
10#include <linux/parser.h>
b17500a0 11#include <linux/seq_file.h>
6af7b483 12#include "xattr.h"
ba2b77a8 13
13f06f48
CY
14#define CREATE_TRACE_POINTS
15#include <trace/events/erofs.h>
16
ba2b77a8
GX
17static struct kmem_cache *erofs_inode_cachep __read_mostly;
18
99634bf3 19static void erofs_inode_init_once(void *ptr)
ba2b77a8 20{
a5876e24 21 struct erofs_inode *vi = ptr;
ba2b77a8
GX
22
23 inode_init_once(&vi->vfs_inode);
24}
25
99634bf3 26static struct inode *erofs_alloc_inode(struct super_block *sb)
ba2b77a8 27{
a5876e24 28 struct erofs_inode *vi =
ba2b77a8
GX
29 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
30
e2ff9f15 31 if (!vi)
ba2b77a8
GX
32 return NULL;
33
34 /* zero out everything except vfs_inode */
a5876e24 35 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
ba2b77a8
GX
36 return &vi->vfs_inode;
37}
38
99634bf3 39static void erofs_free_inode(struct inode *inode)
ba2b77a8 40{
a5876e24 41 struct erofs_inode *vi = EROFS_I(inode);
ba2b77a8 42
a2c75c81
GX
43 /* be careful of RCU symlink path */
44 if (inode->i_op == &erofs_fast_symlink_iops)
ba2b77a8 45 kfree(inode->i_link);
ba2b77a8
GX
46 kfree(vi->xattr_shared_xattrs);
47
48 kmem_cache_free(erofs_inode_cachep, vi);
49}
50
5efe5137 51static bool check_layout_compatibility(struct super_block *sb,
0259f209 52 struct erofs_super_block *dsb)
5efe5137 53{
0259f209 54 const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
5efe5137 55
426a9308 56 EROFS_SB(sb)->feature_incompat = feature;
5efe5137
GX
57
58 /* check if current kernel meets all mandatory requirements */
426a9308
GX
59 if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
60 errln("unidentified incompatible feature %x, please upgrade kernel version",
61 feature & ~EROFS_ALL_FEATURE_INCOMPAT);
5efe5137
GX
62 return false;
63 }
64 return true;
65}
66
99634bf3 67static int erofs_read_superblock(struct super_block *sb)
ba2b77a8
GX
68{
69 struct erofs_sb_info *sbi;
70 struct buffer_head *bh;
0259f209 71 struct erofs_super_block *dsb;
7dd68b14 72 unsigned int blkszbits;
ba2b77a8
GX
73 int ret;
74
75 bh = sb_bread(sb, 0);
76
e2ff9f15 77 if (!bh) {
ba2b77a8
GX
78 errln("cannot read erofs superblock");
79 return -EIO;
80 }
81
82 sbi = EROFS_SB(sb);
0259f209 83 dsb = (struct erofs_super_block *)(bh->b_data + EROFS_SUPER_OFFSET);
ba2b77a8
GX
84
85 ret = -EINVAL;
0259f209 86 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
ba2b77a8
GX
87 errln("cannot find valid erofs superblock");
88 goto out;
89 }
90
0259f209 91 blkszbits = dsb->blkszbits;
ba2b77a8 92 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
8d8a09b0 93 if (blkszbits != LOG_BLOCK_SIZE) {
ba2b77a8 94 errln("blksize %u isn't supported on this platform",
447a3621 95 1 << blkszbits);
ba2b77a8
GX
96 goto out;
97 }
98
0259f209 99 if (!check_layout_compatibility(sb, dsb))
5efe5137
GX
100 goto out;
101
0259f209
GX
102 sbi->blocks = le32_to_cpu(dsb->blocks);
103 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
b17500a0 104#ifdef CONFIG_EROFS_FS_XATTR
0259f209 105 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
b17500a0 106#endif
8a765682 107 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
0259f209
GX
108 sbi->root_nid = le16_to_cpu(dsb->root_nid);
109 sbi->inos = le64_to_cpu(dsb->inos);
ba2b77a8 110
0259f209
GX
111 sbi->build_time = le64_to_cpu(dsb->build_time);
112 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
ba2b77a8 113
0259f209 114 memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
ba2b77a8 115
0259f209
GX
116 ret = strscpy(sbi->volume_name, dsb->volume_name,
117 sizeof(dsb->volume_name));
a64d9493
GX
118 if (ret < 0) { /* -E2BIG */
119 errln("bad volume name without NIL terminator");
120 ret = -EFSCORRUPTED;
121 goto out;
122 }
ba2b77a8
GX
123 ret = 0;
124out:
125 brelse(bh);
126 return ret;
127}
128
4279f3f9
GX
129#ifdef CONFIG_EROFS_FS_ZIP
130static int erofs_build_cache_strategy(struct erofs_sb_info *sbi,
131 substring_t *args)
132{
133 const char *cs = match_strdup(args);
134 int err = 0;
135
136 if (!cs) {
137 errln("Not enough memory to store cache strategy");
138 return -ENOMEM;
139 }
140
141 if (!strcmp(cs, "disabled")) {
142 sbi->cache_strategy = EROFS_ZIP_CACHE_DISABLED;
143 } else if (!strcmp(cs, "readahead")) {
144 sbi->cache_strategy = EROFS_ZIP_CACHE_READAHEAD;
145 } else if (!strcmp(cs, "readaround")) {
146 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
147 } else {
148 errln("Unrecognized cache strategy \"%s\"", cs);
149 err = -EINVAL;
150 }
151 kfree(cs);
152 return err;
153}
154#else
155static int erofs_build_cache_strategy(struct erofs_sb_info *sbi,
156 substring_t *args)
157{
158 infoln("EROFS compression is disabled, so cache strategy is ignored");
159 return 0;
160}
161#endif
162
163/* set up default EROFS parameters */
99634bf3 164static void erofs_default_options(struct erofs_sb_info *sbi)
ba2b77a8 165{
5fb76bb0 166#ifdef CONFIG_EROFS_FS_ZIP
4279f3f9 167 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
14f362b4 168 sbi->max_sync_decompress_pages = 3;
5fb76bb0 169#endif
b17500a0
GX
170#ifdef CONFIG_EROFS_FS_XATTR
171 set_opt(sbi, XATTR_USER);
172#endif
b17500a0
GX
173#ifdef CONFIG_EROFS_FS_POSIX_ACL
174 set_opt(sbi, POSIX_ACL);
175#endif
ba2b77a8
GX
176}
177
178enum {
b17500a0
GX
179 Opt_user_xattr,
180 Opt_nouser_xattr,
181 Opt_acl,
182 Opt_noacl,
4279f3f9 183 Opt_cache_strategy,
ba2b77a8
GX
184 Opt_err
185};
186
187static match_table_t erofs_tokens = {
b17500a0
GX
188 {Opt_user_xattr, "user_xattr"},
189 {Opt_nouser_xattr, "nouser_xattr"},
190 {Opt_acl, "acl"},
191 {Opt_noacl, "noacl"},
4279f3f9 192 {Opt_cache_strategy, "cache_strategy=%s"},
ba2b77a8
GX
193 {Opt_err, NULL}
194};
195
99634bf3 196static int erofs_parse_options(struct super_block *sb, char *options)
ba2b77a8
GX
197{
198 substring_t args[MAX_OPT_ARGS];
199 char *p;
01e4ae4b 200 int err;
ba2b77a8
GX
201
202 if (!options)
203 return 0;
204
561fb35a 205 while ((p = strsep(&options, ","))) {
ba2b77a8
GX
206 int token;
207
208 if (!*p)
209 continue;
210
211 args[0].to = args[0].from = NULL;
212 token = match_token(p, erofs_tokens, args);
213
214 switch (token) {
b17500a0
GX
215#ifdef CONFIG_EROFS_FS_XATTR
216 case Opt_user_xattr:
217 set_opt(EROFS_SB(sb), XATTR_USER);
218 break;
219 case Opt_nouser_xattr:
220 clear_opt(EROFS_SB(sb), XATTR_USER);
221 break;
222#else
223 case Opt_user_xattr:
224 infoln("user_xattr options not supported");
225 break;
226 case Opt_nouser_xattr:
227 infoln("nouser_xattr options not supported");
228 break;
229#endif
230#ifdef CONFIG_EROFS_FS_POSIX_ACL
231 case Opt_acl:
232 set_opt(EROFS_SB(sb), POSIX_ACL);
233 break;
234 case Opt_noacl:
235 clear_opt(EROFS_SB(sb), POSIX_ACL);
236 break;
237#else
238 case Opt_acl:
239 infoln("acl options not supported");
240 break;
241 case Opt_noacl:
242 infoln("noacl options not supported");
243 break;
244#endif
4279f3f9
GX
245 case Opt_cache_strategy:
246 err = erofs_build_cache_strategy(EROFS_SB(sb), args);
247 if (err)
248 return err;
249 break;
ba2b77a8 250 default:
bc33d9f3 251 errln("Unrecognized mount option \"%s\" or missing value", p);
ba2b77a8
GX
252 return -EINVAL;
253 }
254 }
255 return 0;
256}
257
4279f3f9 258#ifdef CONFIG_EROFS_FS_ZIP
105d4ad8
GX
259static const struct address_space_operations managed_cache_aops;
260
99634bf3 261static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
105d4ad8
GX
262{
263 int ret = 1; /* 0 - busy */
264 struct address_space *const mapping = page->mapping;
265
8b987bca
GX
266 DBG_BUGON(!PageLocked(page));
267 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
105d4ad8
GX
268
269 if (PagePrivate(page))
47e541a1 270 ret = erofs_try_to_free_cached_page(mapping, page);
105d4ad8
GX
271
272 return ret;
273}
274
99634bf3
GX
275static void erofs_managed_cache_invalidatepage(struct page *page,
276 unsigned int offset,
277 unsigned int length)
105d4ad8
GX
278{
279 const unsigned int stop = length + offset;
280
8b987bca 281 DBG_BUGON(!PageLocked(page));
105d4ad8 282
8b987bca
GX
283 /* Check for potential overflow in debug mode */
284 DBG_BUGON(stop > PAGE_SIZE || stop < length);
105d4ad8
GX
285
286 if (offset == 0 && stop == PAGE_SIZE)
99634bf3 287 while (!erofs_managed_cache_releasepage(page, GFP_NOFS))
105d4ad8
GX
288 cond_resched();
289}
290
291static const struct address_space_operations managed_cache_aops = {
99634bf3
GX
292 .releasepage = erofs_managed_cache_releasepage,
293 .invalidatepage = erofs_managed_cache_invalidatepage,
105d4ad8
GX
294};
295
8f7acdae 296static int erofs_init_managed_cache(struct super_block *sb)
105d4ad8 297{
8f7acdae
GX
298 struct erofs_sb_info *const sbi = EROFS_SB(sb);
299 struct inode *const inode = new_inode(sb);
105d4ad8 300
8d8a09b0 301 if (!inode)
8f7acdae 302 return -ENOMEM;
105d4ad8
GX
303
304 set_nlink(inode, 1);
305 inode->i_size = OFFSET_MAX;
306
307 inode->i_mapping->a_ops = &managed_cache_aops;
308 mapping_set_gfp_mask(inode->i_mapping,
8494c29f 309 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
8f7acdae
GX
310 sbi->managed_cache = inode;
311 return 0;
105d4ad8 312}
8f7acdae
GX
313#else
314static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
105d4ad8
GX
315#endif
316
9e794de5 317static int erofs_fill_super(struct super_block *sb, void *data, int silent)
ba2b77a8
GX
318{
319 struct inode *inode;
320 struct erofs_sb_info *sbi;
8f7acdae 321 int err;
ba2b77a8 322
8f7acdae
GX
323 sb->s_magic = EROFS_SUPER_MAGIC;
324
8d8a09b0 325 if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
ba2b77a8 326 errln("failed to set erofs blksize");
8f7acdae 327 return -EINVAL;
ba2b77a8
GX
328 }
329
a9f69bd5 330 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
8d8a09b0 331 if (!sbi)
8f7acdae 332 return -ENOMEM;
ba2b77a8 333
8f7acdae 334 sb->s_fs_info = sbi;
99634bf3 335 err = erofs_read_superblock(sb);
ba2b77a8 336 if (err)
8f7acdae 337 return err;
ba2b77a8 338
5f0abea6 339 sb->s_flags |= SB_RDONLY | SB_NOATIME;
ba2b77a8
GX
340 sb->s_maxbytes = MAX_LFS_FILESIZE;
341 sb->s_time_gran = 1;
342
343 sb->s_op = &erofs_sops;
344
b17500a0
GX
345#ifdef CONFIG_EROFS_FS_XATTR
346 sb->s_xattr = erofs_xattr_handlers;
347#endif
ba2b77a8 348 /* set erofs default mount options */
99634bf3 349 erofs_default_options(sbi);
ba2b77a8 350
99634bf3 351 err = erofs_parse_options(sb, data);
8d8a09b0 352 if (err)
8f7acdae 353 return err;
ba2b77a8 354
516c115c
GX
355 if (test_opt(sbi, POSIX_ACL))
356 sb->s_flags |= SB_POSIXACL;
357 else
358 sb->s_flags &= ~SB_POSIXACL;
359
e7e9a307
GX
360#ifdef CONFIG_EROFS_FS_ZIP
361 INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
362#endif
363
ba2b77a8
GX
364 /* get the root inode */
365 inode = erofs_iget(sb, ROOT_NID(sbi), true);
8f7acdae
GX
366 if (IS_ERR(inode))
367 return PTR_ERR(inode);
ba2b77a8 368
8d8a09b0 369 if (!S_ISDIR(inode->i_mode)) {
ba2b77a8 370 errln("rootino(nid %llu) is not a directory(i_mode %o)",
447a3621 371 ROOT_NID(sbi), inode->i_mode);
94832d93 372 iput(inode);
8f7acdae 373 return -EINVAL;
ba2b77a8
GX
374 }
375
376 sb->s_root = d_make_root(inode);
8d8a09b0 377 if (!sb->s_root)
8f7acdae 378 return -ENOMEM;
ba2b77a8 379
22fe04a7 380 erofs_shrinker_register(sb);
8f7acdae
GX
381 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
382 err = erofs_init_managed_cache(sb);
8d8a09b0 383 if (err)
8f7acdae 384 return err;
2497ee41 385
ba2b77a8 386 if (!silent)
688a5f2e
GX
387 infoln("mounted on %s with opts: %s, root inode @ nid %llu.",
388 sb->s_id, (char *)data, ROOT_NID(sbi));
ba2b77a8 389 return 0;
8f7acdae
GX
390}
391
392static struct dentry *erofs_mount(struct file_system_type *fs_type, int flags,
393 const char *dev_name, void *data)
394{
395 return mount_bdev(fs_type, flags, dev_name, data, erofs_fill_super);
ba2b77a8
GX
396}
397
398/*
399 * could be triggered after deactivate_locked_super()
400 * is called, thus including umount and failed to initialize.
401 */
8f7acdae 402static void erofs_kill_sb(struct super_block *sb)
ba2b77a8 403{
8f7acdae
GX
404 struct erofs_sb_info *sbi;
405
406 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
407 infoln("unmounting for %s", sb->s_id);
ba2b77a8 408
8f7acdae
GX
409 kill_block_super(sb);
410
411 sbi = EROFS_SB(sb);
e2ff9f15 412 if (!sbi)
ba2b77a8 413 return;
8f7acdae
GX
414 kfree(sbi);
415 sb->s_fs_info = NULL;
416}
ba2b77a8 417
8f7acdae
GX
418/* called when ->s_root is non-NULL */
419static void erofs_put_super(struct super_block *sb)
420{
421 struct erofs_sb_info *const sbi = EROFS_SB(sb);
ba2b77a8 422
8f7acdae 423 DBG_BUGON(!sbi);
ba2b77a8 424
22fe04a7 425 erofs_shrinker_unregister(sb);
4279f3f9 426#ifdef CONFIG_EROFS_FS_ZIP
105d4ad8 427 iput(sbi->managed_cache);
8f7acdae 428 sbi->managed_cache = NULL;
105d4ad8 429#endif
ba2b77a8
GX
430}
431
ba2b77a8
GX
432static struct file_system_type erofs_fs_type = {
433 .owner = THIS_MODULE,
434 .name = "erofs",
435 .mount = erofs_mount,
8f7acdae 436 .kill_sb = erofs_kill_sb,
ba2b77a8
GX
437 .fs_flags = FS_REQUIRES_DEV,
438};
439MODULE_ALIAS_FS("erofs");
440
441static int __init erofs_module_init(void)
442{
443 int err;
444
445 erofs_check_ondisk_layout_definitions();
446 infoln("initializing erofs " EROFS_VERSION);
447
1c2dfbf9 448 erofs_inode_cachep = kmem_cache_create("erofs_inode",
a5876e24 449 sizeof(struct erofs_inode), 0,
1c2dfbf9 450 SLAB_RECLAIM_ACCOUNT,
99634bf3 451 erofs_inode_init_once);
1c2dfbf9
GX
452 if (!erofs_inode_cachep) {
453 err = -ENOMEM;
ba2b77a8 454 goto icache_err;
1c2dfbf9 455 }
ba2b77a8 456
22fe04a7 457 err = erofs_init_shrinker();
a1581312
GX
458 if (err)
459 goto shrinker_err;
460
3883a79a
GX
461 err = z_erofs_init_zip_subsystem();
462 if (err)
463 goto zip_err;
3883a79a 464
ba2b77a8
GX
465 err = register_filesystem(&erofs_fs_type);
466 if (err)
467 goto fs_err;
468
469 infoln("successfully to initialize erofs");
470 return 0;
471
472fs_err:
3883a79a
GX
473 z_erofs_exit_zip_subsystem();
474zip_err:
22fe04a7 475 erofs_exit_shrinker();
a1581312 476shrinker_err:
1c2dfbf9 477 kmem_cache_destroy(erofs_inode_cachep);
ba2b77a8
GX
478icache_err:
479 return err;
480}
481
482static void __exit erofs_module_exit(void)
483{
484 unregister_filesystem(&erofs_fs_type);
3883a79a 485 z_erofs_exit_zip_subsystem();
22fe04a7 486 erofs_exit_shrinker();
1c2dfbf9
GX
487
488 /* Ensure all RCU free inodes are safe before cache is destroyed. */
489 rcu_barrier();
490 kmem_cache_destroy(erofs_inode_cachep);
ba2b77a8
GX
491 infoln("successfully finalize erofs");
492}
493
494/* get filesystem statistics */
495static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
496{
497 struct super_block *sb = dentry->d_sb;
498 struct erofs_sb_info *sbi = EROFS_SB(sb);
499 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
500
501 buf->f_type = sb->s_magic;
502 buf->f_bsize = EROFS_BLKSIZ;
503 buf->f_blocks = sbi->blocks;
504 buf->f_bfree = buf->f_bavail = 0;
505
506 buf->f_files = ULLONG_MAX;
507 buf->f_ffree = ULLONG_MAX - sbi->inos;
508
509 buf->f_namelen = EROFS_NAME_LEN;
510
511 buf->f_fsid.val[0] = (u32)id;
512 buf->f_fsid.val[1] = (u32)(id >> 32);
513 return 0;
514}
515
516static int erofs_show_options(struct seq_file *seq, struct dentry *root)
517{
b17500a0
GX
518 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
519
520#ifdef CONFIG_EROFS_FS_XATTR
521 if (test_opt(sbi, XATTR_USER))
522 seq_puts(seq, ",user_xattr");
523 else
524 seq_puts(seq, ",nouser_xattr");
525#endif
526#ifdef CONFIG_EROFS_FS_POSIX_ACL
527 if (test_opt(sbi, POSIX_ACL))
528 seq_puts(seq, ",acl");
529 else
530 seq_puts(seq, ",noacl");
9c07b3b3 531#endif
4279f3f9
GX
532#ifdef CONFIG_EROFS_FS_ZIP
533 if (sbi->cache_strategy == EROFS_ZIP_CACHE_DISABLED) {
534 seq_puts(seq, ",cache_strategy=disabled");
535 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAHEAD) {
536 seq_puts(seq, ",cache_strategy=readahead");
537 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAROUND) {
538 seq_puts(seq, ",cache_strategy=readaround");
539 } else {
540 seq_puts(seq, ",cache_strategy=(unknown)");
541 DBG_BUGON(1);
542 }
543#endif
ba2b77a8
GX
544 return 0;
545}
546
547static int erofs_remount(struct super_block *sb, int *flags, char *data)
548{
d41076ea
CX
549 struct erofs_sb_info *sbi = EROFS_SB(sb);
550 unsigned int org_mnt_opt = sbi->mount_opt;
d41076ea
CX
551 int err;
552
8b987bca 553 DBG_BUGON(!sb_rdonly(sb));
99634bf3 554 err = erofs_parse_options(sb, data);
d41076ea
CX
555 if (err)
556 goto out;
ba2b77a8 557
516c115c
GX
558 if (test_opt(sbi, POSIX_ACL))
559 sb->s_flags |= SB_POSIXACL;
560 else
561 sb->s_flags &= ~SB_POSIXACL;
562
5f0abea6 563 *flags |= SB_RDONLY;
ba2b77a8 564 return 0;
d41076ea 565out:
d41076ea 566 sbi->mount_opt = org_mnt_opt;
d41076ea 567 return err;
ba2b77a8
GX
568}
569
570const struct super_operations erofs_sops = {
571 .put_super = erofs_put_super,
99634bf3
GX
572 .alloc_inode = erofs_alloc_inode,
573 .free_inode = erofs_free_inode,
ba2b77a8
GX
574 .statfs = erofs_statfs,
575 .show_options = erofs_show_options,
576 .remount_fs = erofs_remount,
577};
578
579module_init(erofs_module_init);
580module_exit(erofs_module_exit);
581
582MODULE_DESCRIPTION("Enhanced ROM File System");
bc33d9f3 583MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
ba2b77a8
GX
584MODULE_LICENSE("GPL");
585