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