GNU Linux-libre 4.19.207-gnu1
[releases.git] / drivers / staging / erofs / super.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/drivers/staging/erofs/super.c
4  *
5  * Copyright (C) 2017-2018 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Gao Xiang <gaoxiang25@huawei.com>
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file COPYING in the main directory of the Linux
11  * distribution for more details.
12  */
13 #include <linux/module.h>
14 #include <linux/buffer_head.h>
15 #include <linux/statfs.h>
16 #include <linux/parser.h>
17 #include <linux/seq_file.h>
18 #include "internal.h"
19
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/erofs.h>
22
23 static struct kmem_cache *erofs_inode_cachep __read_mostly;
24
25 static void init_once(void *ptr)
26 {
27         struct erofs_vnode *vi = ptr;
28
29         inode_init_once(&vi->vfs_inode);
30 }
31
32 static int erofs_init_inode_cache(void)
33 {
34         erofs_inode_cachep = kmem_cache_create("erofs_inode",
35                 sizeof(struct erofs_vnode), 0,
36                 SLAB_RECLAIM_ACCOUNT, init_once);
37
38         return erofs_inode_cachep != NULL ? 0 : -ENOMEM;
39 }
40
41 static void erofs_exit_inode_cache(void)
42 {
43         kmem_cache_destroy(erofs_inode_cachep);
44 }
45
46 static struct inode *alloc_inode(struct super_block *sb)
47 {
48         struct erofs_vnode *vi =
49                 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
50
51         if (vi == NULL)
52                 return NULL;
53
54         /* zero out everything except vfs_inode */
55         memset(vi, 0, offsetof(struct erofs_vnode, vfs_inode));
56         return &vi->vfs_inode;
57 }
58
59 static void i_callback(struct rcu_head *head)
60 {
61         struct inode *inode = container_of(head, struct inode, i_rcu);
62         struct erofs_vnode *vi = EROFS_V(inode);
63
64         /* be careful RCU symlink path (see ext4_inode_info->i_data)! */
65         if (is_inode_fast_symlink(inode))
66                 kfree(inode->i_link);
67
68         kfree(vi->xattr_shared_xattrs);
69
70         kmem_cache_free(erofs_inode_cachep, vi);
71 }
72
73 static void destroy_inode(struct inode *inode)
74 {
75         call_rcu(&inode->i_rcu, i_callback);
76 }
77
78 static bool check_layout_compatibility(struct super_block *sb,
79                                        struct erofs_super_block *layout)
80 {
81         const unsigned int requirements = le32_to_cpu(layout->requirements);
82
83         EROFS_SB(sb)->requirements = requirements;
84
85         /* check if current kernel meets all mandatory requirements */
86         if (requirements & (~EROFS_ALL_REQUIREMENTS)) {
87                 errln("unidentified requirements %x, please upgrade kernel version",
88                       requirements & ~EROFS_ALL_REQUIREMENTS);
89                 return false;
90         }
91         return true;
92 }
93
94 static int superblock_read(struct super_block *sb)
95 {
96         struct erofs_sb_info *sbi;
97         struct buffer_head *bh;
98         struct erofs_super_block *layout;
99         unsigned blkszbits;
100         int ret;
101
102         bh = sb_bread(sb, 0);
103
104         if (bh == NULL) {
105                 errln("cannot read erofs superblock");
106                 return -EIO;
107         }
108
109         sbi = EROFS_SB(sb);
110         layout = (struct erofs_super_block *)((u8 *)bh->b_data
111                  + EROFS_SUPER_OFFSET);
112
113         ret = -EINVAL;
114         if (le32_to_cpu(layout->magic) != EROFS_SUPER_MAGIC_V1) {
115                 errln("cannot find valid erofs superblock");
116                 goto out;
117         }
118
119         blkszbits = layout->blkszbits;
120         /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
121         if (unlikely(blkszbits != LOG_BLOCK_SIZE)) {
122                 errln("blksize %u isn't supported on this platform",
123                         1 << blkszbits);
124                 goto out;
125         }
126
127         if (!check_layout_compatibility(sb, layout))
128                 goto out;
129
130         sbi->blocks = le32_to_cpu(layout->blocks);
131         sbi->meta_blkaddr = le32_to_cpu(layout->meta_blkaddr);
132 #ifdef CONFIG_EROFS_FS_XATTR
133         sbi->xattr_blkaddr = le32_to_cpu(layout->xattr_blkaddr);
134 #endif
135         sbi->islotbits = ffs(sizeof(struct erofs_inode_v1)) - 1;
136 #ifdef CONFIG_EROFS_FS_ZIP
137         sbi->clusterbits = 12;
138
139         if (1 << (sbi->clusterbits - 12) > Z_EROFS_CLUSTER_MAX_PAGES)
140                 errln("clusterbits %u is not supported on this kernel",
141                         sbi->clusterbits);
142 #endif
143
144         sbi->root_nid = le16_to_cpu(layout->root_nid);
145         sbi->inos = le64_to_cpu(layout->inos);
146
147         sbi->build_time = le64_to_cpu(layout->build_time);
148         sbi->build_time_nsec = le32_to_cpu(layout->build_time_nsec);
149
150         memcpy(&sb->s_uuid, layout->uuid, sizeof(layout->uuid));
151         memcpy(sbi->volume_name, layout->volume_name,
152                 sizeof(layout->volume_name));
153
154         ret = 0;
155 out:
156         brelse(bh);
157         return ret;
158 }
159
160 #ifdef CONFIG_EROFS_FAULT_INJECTION
161 char *erofs_fault_name[FAULT_MAX] = {
162         [FAULT_KMALLOC]         = "kmalloc",
163 };
164
165 static void erofs_build_fault_attr(struct erofs_sb_info *sbi,
166                                                 unsigned int rate)
167 {
168         struct erofs_fault_info *ffi = &sbi->fault_info;
169
170         if (rate) {
171                 atomic_set(&ffi->inject_ops, 0);
172                 ffi->inject_rate = rate;
173                 ffi->inject_type = (1 << FAULT_MAX) - 1;
174         } else {
175                 memset(ffi, 0, sizeof(struct erofs_fault_info));
176         }
177 }
178 #endif
179
180 static void default_options(struct erofs_sb_info *sbi)
181 {
182 #ifdef CONFIG_EROFS_FS_XATTR
183         set_opt(sbi, XATTR_USER);
184 #endif
185
186 #ifdef CONFIG_EROFS_FS_POSIX_ACL
187         set_opt(sbi, POSIX_ACL);
188 #endif
189 }
190
191 enum {
192         Opt_user_xattr,
193         Opt_nouser_xattr,
194         Opt_acl,
195         Opt_noacl,
196         Opt_fault_injection,
197         Opt_err
198 };
199
200 static match_table_t erofs_tokens = {
201         {Opt_user_xattr, "user_xattr"},
202         {Opt_nouser_xattr, "nouser_xattr"},
203         {Opt_acl, "acl"},
204         {Opt_noacl, "noacl"},
205         {Opt_fault_injection, "fault_injection=%u"},
206         {Opt_err, NULL}
207 };
208
209 static int parse_options(struct super_block *sb, char *options)
210 {
211         substring_t args[MAX_OPT_ARGS];
212         char *p;
213         int arg = 0;
214
215         if (!options)
216                 return 0;
217
218         while ((p = strsep(&options, ",")) != NULL) {
219                 int token;
220
221                 if (!*p)
222                         continue;
223
224                 args[0].to = args[0].from = NULL;
225                 token = match_token(p, erofs_tokens, args);
226
227                 switch (token) {
228 #ifdef CONFIG_EROFS_FS_XATTR
229                 case Opt_user_xattr:
230                         set_opt(EROFS_SB(sb), XATTR_USER);
231                         break;
232                 case Opt_nouser_xattr:
233                         clear_opt(EROFS_SB(sb), XATTR_USER);
234                         break;
235 #else
236                 case Opt_user_xattr:
237                         infoln("user_xattr options not supported");
238                         break;
239                 case Opt_nouser_xattr:
240                         infoln("nouser_xattr options not supported");
241                         break;
242 #endif
243 #ifdef CONFIG_EROFS_FS_POSIX_ACL
244                 case Opt_acl:
245                         set_opt(EROFS_SB(sb), POSIX_ACL);
246                         break;
247                 case Opt_noacl:
248                         clear_opt(EROFS_SB(sb), POSIX_ACL);
249                         break;
250 #else
251                 case Opt_acl:
252                         infoln("acl options not supported");
253                         break;
254                 case Opt_noacl:
255                         infoln("noacl options not supported");
256                         break;
257 #endif
258                 case Opt_fault_injection:
259                         if (args->from && match_int(args, &arg))
260                                 return -EINVAL;
261 #ifdef CONFIG_EROFS_FAULT_INJECTION
262                         erofs_build_fault_attr(EROFS_SB(sb), arg);
263                         set_opt(EROFS_SB(sb), FAULT_INJECTION);
264 #else
265                         infoln("FAULT_INJECTION was not selected");
266 #endif
267                         break;
268                 default:
269                         errln("Unrecognized mount option \"%s\" "
270                                         "or missing value", p);
271                         return -EINVAL;
272                 }
273         }
274         return 0;
275 }
276
277 #ifdef EROFS_FS_HAS_MANAGED_CACHE
278
279 static const struct address_space_operations managed_cache_aops;
280
281 static int managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
282 {
283         int ret = 1;    /* 0 - busy */
284         struct address_space *const mapping = page->mapping;
285
286         DBG_BUGON(!PageLocked(page));
287         DBG_BUGON(mapping->a_ops != &managed_cache_aops);
288
289         if (PagePrivate(page))
290                 ret = erofs_try_to_free_cached_page(mapping, page);
291
292         return ret;
293 }
294
295 static void managed_cache_invalidatepage(struct page *page,
296         unsigned int offset, unsigned int length)
297 {
298         const unsigned int stop = length + offset;
299
300         DBG_BUGON(!PageLocked(page));
301
302         /* Check for potential overflow in debug mode */
303         DBG_BUGON(stop > PAGE_SIZE || stop < length);
304
305         if (offset == 0 && stop == PAGE_SIZE)
306                 while (!managed_cache_releasepage(page, GFP_NOFS))
307                         cond_resched();
308 }
309
310 static const struct address_space_operations managed_cache_aops = {
311         .releasepage = managed_cache_releasepage,
312         .invalidatepage = managed_cache_invalidatepage,
313 };
314
315 static struct inode *erofs_init_managed_cache(struct super_block *sb)
316 {
317         struct inode *inode = new_inode(sb);
318
319         if (unlikely(inode == NULL))
320                 return ERR_PTR(-ENOMEM);
321
322         set_nlink(inode, 1);
323         inode->i_size = OFFSET_MAX;
324
325         inode->i_mapping->a_ops = &managed_cache_aops;
326         mapping_set_gfp_mask(inode->i_mapping,
327                              GFP_NOFS | __GFP_HIGHMEM |
328                              __GFP_MOVABLE |  __GFP_NOFAIL);
329         return inode;
330 }
331
332 #endif
333
334 static int erofs_read_super(struct super_block *sb,
335         const char *dev_name, void *data, int silent)
336 {
337         struct inode *inode;
338         struct erofs_sb_info *sbi;
339         int err = -EINVAL;
340
341         infoln("read_super, device -> %s", dev_name);
342         infoln("options -> %s", (char *)data);
343
344         if (unlikely(!sb_set_blocksize(sb, EROFS_BLKSIZ))) {
345                 errln("failed to set erofs blksize");
346                 goto err;
347         }
348
349         sbi = kzalloc(sizeof(struct erofs_sb_info), GFP_KERNEL);
350         if (unlikely(sbi == NULL)) {
351                 err = -ENOMEM;
352                 goto err;
353         }
354         sb->s_fs_info = sbi;
355
356         err = superblock_read(sb);
357         if (err)
358                 goto err_sbread;
359
360         sb->s_magic = EROFS_SUPER_MAGIC;
361         sb->s_flags |= SB_RDONLY | SB_NOATIME;
362         sb->s_maxbytes = MAX_LFS_FILESIZE;
363         sb->s_time_gran = 1;
364
365         sb->s_op = &erofs_sops;
366
367 #ifdef CONFIG_EROFS_FS_XATTR
368         sb->s_xattr = erofs_xattr_handlers;
369 #endif
370
371         /* set erofs default mount options */
372         default_options(sbi);
373
374         err = parse_options(sb, data);
375         if (err)
376                 goto err_parseopt;
377
378         if (!silent)
379                 infoln("root inode @ nid %llu", ROOT_NID(sbi));
380
381 #ifdef CONFIG_EROFS_FS_ZIP
382         INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
383 #endif
384
385 #ifdef EROFS_FS_HAS_MANAGED_CACHE
386         sbi->managed_cache = erofs_init_managed_cache(sb);
387         if (IS_ERR(sbi->managed_cache)) {
388                 err = PTR_ERR(sbi->managed_cache);
389                 goto err_init_managed_cache;
390         }
391 #endif
392
393         /* get the root inode */
394         inode = erofs_iget(sb, ROOT_NID(sbi), true);
395         if (IS_ERR(inode)) {
396                 err = PTR_ERR(inode);
397                 goto err_iget;
398         }
399
400         if (!S_ISDIR(inode->i_mode)) {
401                 errln("rootino(nid %llu) is not a directory(i_mode %o)",
402                         ROOT_NID(sbi), inode->i_mode);
403                 err = -EINVAL;
404                 goto err_isdir;
405         }
406
407         sb->s_root = d_make_root(inode);
408         if (sb->s_root == NULL) {
409                 err = -ENOMEM;
410                 goto err_makeroot;
411         }
412
413         /* save the device name to sbi */
414         sbi->dev_name = __getname();
415         if (sbi->dev_name == NULL) {
416                 err = -ENOMEM;
417                 goto err_devname;
418         }
419
420         snprintf(sbi->dev_name, PATH_MAX, "%s", dev_name);
421         sbi->dev_name[PATH_MAX - 1] = '\0';
422
423         erofs_register_super(sb);
424
425         if (!silent)
426                 infoln("mounted on %s with opts: %s.", dev_name,
427                         (char *)data);
428         return 0;
429         /*
430          * please add a label for each exit point and use
431          * the following name convention, thus new features
432          * can be integrated easily without renaming labels.
433          */
434 err_devname:
435         dput(sb->s_root);
436 err_makeroot:
437 err_isdir:
438         if (sb->s_root == NULL)
439                 iput(inode);
440 err_iget:
441 #ifdef EROFS_FS_HAS_MANAGED_CACHE
442         iput(sbi->managed_cache);
443 err_init_managed_cache:
444 #endif
445 err_parseopt:
446 err_sbread:
447         sb->s_fs_info = NULL;
448         kfree(sbi);
449 err:
450         return err;
451 }
452
453 /*
454  * could be triggered after deactivate_locked_super()
455  * is called, thus including umount and failed to initialize.
456  */
457 static void erofs_put_super(struct super_block *sb)
458 {
459         struct erofs_sb_info *sbi = EROFS_SB(sb);
460
461         /* for cases which are failed in "read_super" */
462         if (sbi == NULL)
463                 return;
464
465         WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
466
467         infoln("unmounted for %s", sbi->dev_name);
468         __putname(sbi->dev_name);
469
470 #ifdef EROFS_FS_HAS_MANAGED_CACHE
471         iput(sbi->managed_cache);
472 #endif
473
474         mutex_lock(&sbi->umount_mutex);
475
476 #ifdef CONFIG_EROFS_FS_ZIP
477         erofs_workstation_cleanup_all(sb);
478 #endif
479
480         erofs_unregister_super(sb);
481         mutex_unlock(&sbi->umount_mutex);
482
483         kfree(sbi);
484         sb->s_fs_info = NULL;
485 }
486
487
488 struct erofs_mount_private {
489         const char *dev_name;
490         char *options;
491 };
492
493 /* support mount_bdev() with options */
494 static int erofs_fill_super(struct super_block *sb,
495         void *_priv, int silent)
496 {
497         struct erofs_mount_private *priv = _priv;
498
499         return erofs_read_super(sb, priv->dev_name,
500                 priv->options, silent);
501 }
502
503 static struct dentry *erofs_mount(
504         struct file_system_type *fs_type, int flags,
505         const char *dev_name, void *data)
506 {
507         struct erofs_mount_private priv = {
508                 .dev_name = dev_name,
509                 .options = data
510         };
511
512         return mount_bdev(fs_type, flags, dev_name,
513                 &priv, erofs_fill_super);
514 }
515
516 static void erofs_kill_sb(struct super_block *sb)
517 {
518         kill_block_super(sb);
519 }
520
521 static struct shrinker erofs_shrinker_info = {
522         .scan_objects = erofs_shrink_scan,
523         .count_objects = erofs_shrink_count,
524         .seeks = DEFAULT_SEEKS,
525 };
526
527 static struct file_system_type erofs_fs_type = {
528         .owner          = THIS_MODULE,
529         .name           = "erofs",
530         .mount          = erofs_mount,
531         .kill_sb        = erofs_kill_sb,
532         .fs_flags       = FS_REQUIRES_DEV,
533 };
534 MODULE_ALIAS_FS("erofs");
535
536 #ifdef CONFIG_EROFS_FS_ZIP
537 extern int z_erofs_init_zip_subsystem(void);
538 extern void z_erofs_exit_zip_subsystem(void);
539 #endif
540
541 static int __init erofs_module_init(void)
542 {
543         int err;
544
545         erofs_check_ondisk_layout_definitions();
546         infoln("initializing erofs " EROFS_VERSION);
547
548         err = erofs_init_inode_cache();
549         if (err)
550                 goto icache_err;
551
552         err = register_shrinker(&erofs_shrinker_info);
553         if (err)
554                 goto shrinker_err;
555
556 #ifdef CONFIG_EROFS_FS_ZIP
557         err = z_erofs_init_zip_subsystem();
558         if (err)
559                 goto zip_err;
560 #endif
561
562         err = register_filesystem(&erofs_fs_type);
563         if (err)
564                 goto fs_err;
565
566         infoln("successfully to initialize erofs");
567         return 0;
568
569 fs_err:
570 #ifdef CONFIG_EROFS_FS_ZIP
571         z_erofs_exit_zip_subsystem();
572 zip_err:
573 #endif
574         unregister_shrinker(&erofs_shrinker_info);
575 shrinker_err:
576         erofs_exit_inode_cache();
577 icache_err:
578         return err;
579 }
580
581 static void __exit erofs_module_exit(void)
582 {
583         unregister_filesystem(&erofs_fs_type);
584 #ifdef CONFIG_EROFS_FS_ZIP
585         z_erofs_exit_zip_subsystem();
586 #endif
587         unregister_shrinker(&erofs_shrinker_info);
588         erofs_exit_inode_cache();
589         infoln("successfully finalize erofs");
590 }
591
592 /* get filesystem statistics */
593 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
594 {
595         struct super_block *sb = dentry->d_sb;
596         struct erofs_sb_info *sbi = EROFS_SB(sb);
597         u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
598
599         buf->f_type = sb->s_magic;
600         buf->f_bsize = EROFS_BLKSIZ;
601         buf->f_blocks = sbi->blocks;
602         buf->f_bfree = buf->f_bavail = 0;
603
604         buf->f_files = ULLONG_MAX;
605         buf->f_ffree = ULLONG_MAX - sbi->inos;
606
607         buf->f_namelen = EROFS_NAME_LEN;
608
609         buf->f_fsid.val[0] = (u32)id;
610         buf->f_fsid.val[1] = (u32)(id >> 32);
611         return 0;
612 }
613
614 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
615 {
616         struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
617
618 #ifdef CONFIG_EROFS_FS_XATTR
619         if (test_opt(sbi, XATTR_USER))
620                 seq_puts(seq, ",user_xattr");
621         else
622                 seq_puts(seq, ",nouser_xattr");
623 #endif
624 #ifdef CONFIG_EROFS_FS_POSIX_ACL
625         if (test_opt(sbi, POSIX_ACL))
626                 seq_puts(seq, ",acl");
627         else
628                 seq_puts(seq, ",noacl");
629 #endif
630 #ifdef CONFIG_EROFS_FAULT_INJECTION
631         if (test_opt(sbi, FAULT_INJECTION))
632                 seq_printf(seq, ",fault_injection=%u",
633                                 sbi->fault_info.inject_rate);
634 #endif
635         return 0;
636 }
637
638 static int erofs_remount(struct super_block *sb, int *flags, char *data)
639 {
640         DBG_BUGON(!sb_rdonly(sb));
641
642         *flags |= SB_RDONLY;
643         return 0;
644 }
645
646 const struct super_operations erofs_sops = {
647         .put_super = erofs_put_super,
648         .alloc_inode = alloc_inode,
649         .destroy_inode = destroy_inode,
650         .statfs = erofs_statfs,
651         .show_options = erofs_show_options,
652         .remount_fs = erofs_remount,
653 };
654
655 module_init(erofs_module_init);
656 module_exit(erofs_module_exit);
657
658 MODULE_DESCRIPTION("Enhanced ROM File System");
659 MODULE_AUTHOR("Gao Xiang, Yu Chao, Miao Xie, CONSUMER BG, HUAWEI Inc.");
660 MODULE_LICENSE("GPL");
661