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