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