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