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