Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * super.c
0004  *
0005  * Copyright (c) 1999 Al Smith
0006  *
0007  * Portions derived from work (c) 1995,1996 Christian Vogelgsang.
0008  */
0009 
0010 #include <linux/init.h>
0011 #include <linux/module.h>
0012 #include <linux/exportfs.h>
0013 #include <linux/slab.h>
0014 #include <linux/buffer_head.h>
0015 #include <linux/vfs.h>
0016 #include <linux/blkdev.h>
0017 
0018 #include "efs.h"
0019 #include <linux/efs_vh.h>
0020 #include <linux/efs_fs_sb.h>
0021 
0022 static int efs_statfs(struct dentry *dentry, struct kstatfs *buf);
0023 static int efs_fill_super(struct super_block *s, void *d, int silent);
0024 
0025 static struct dentry *efs_mount(struct file_system_type *fs_type,
0026     int flags, const char *dev_name, void *data)
0027 {
0028     return mount_bdev(fs_type, flags, dev_name, data, efs_fill_super);
0029 }
0030 
0031 static void efs_kill_sb(struct super_block *s)
0032 {
0033     struct efs_sb_info *sbi = SUPER_INFO(s);
0034     kill_block_super(s);
0035     kfree(sbi);
0036 }
0037 
0038 static struct file_system_type efs_fs_type = {
0039     .owner      = THIS_MODULE,
0040     .name       = "efs",
0041     .mount      = efs_mount,
0042     .kill_sb    = efs_kill_sb,
0043     .fs_flags   = FS_REQUIRES_DEV,
0044 };
0045 MODULE_ALIAS_FS("efs");
0046 
0047 static struct pt_types sgi_pt_types[] = {
0048     {0x00,      "SGI vh"},
0049     {0x01,      "SGI trkrepl"},
0050     {0x02,      "SGI secrepl"},
0051     {0x03,      "SGI raw"},
0052     {0x04,      "SGI bsd"},
0053     {SGI_SYSV,  "SGI sysv"},
0054     {0x06,      "SGI vol"},
0055     {SGI_EFS,   "SGI efs"},
0056     {0x08,      "SGI lv"},
0057     {0x09,      "SGI rlv"},
0058     {0x0A,      "SGI xfs"},
0059     {0x0B,      "SGI xfslog"},
0060     {0x0C,      "SGI xlv"},
0061     {0x82,      "Linux swap"},
0062     {0x83,      "Linux native"},
0063     {0,     NULL}
0064 };
0065 
0066 
0067 static struct kmem_cache * efs_inode_cachep;
0068 
0069 static struct inode *efs_alloc_inode(struct super_block *sb)
0070 {
0071     struct efs_inode_info *ei;
0072     ei = alloc_inode_sb(sb, efs_inode_cachep, GFP_KERNEL);
0073     if (!ei)
0074         return NULL;
0075     return &ei->vfs_inode;
0076 }
0077 
0078 static void efs_free_inode(struct inode *inode)
0079 {
0080     kmem_cache_free(efs_inode_cachep, INODE_INFO(inode));
0081 }
0082 
0083 static void init_once(void *foo)
0084 {
0085     struct efs_inode_info *ei = (struct efs_inode_info *) foo;
0086 
0087     inode_init_once(&ei->vfs_inode);
0088 }
0089 
0090 static int __init init_inodecache(void)
0091 {
0092     efs_inode_cachep = kmem_cache_create("efs_inode_cache",
0093                 sizeof(struct efs_inode_info), 0,
0094                 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
0095                 SLAB_ACCOUNT, init_once);
0096     if (efs_inode_cachep == NULL)
0097         return -ENOMEM;
0098     return 0;
0099 }
0100 
0101 static void destroy_inodecache(void)
0102 {
0103     /*
0104      * Make sure all delayed rcu free inodes are flushed before we
0105      * destroy cache.
0106      */
0107     rcu_barrier();
0108     kmem_cache_destroy(efs_inode_cachep);
0109 }
0110 
0111 static int efs_remount(struct super_block *sb, int *flags, char *data)
0112 {
0113     sync_filesystem(sb);
0114     *flags |= SB_RDONLY;
0115     return 0;
0116 }
0117 
0118 static const struct super_operations efs_superblock_operations = {
0119     .alloc_inode    = efs_alloc_inode,
0120     .free_inode = efs_free_inode,
0121     .statfs     = efs_statfs,
0122     .remount_fs = efs_remount,
0123 };
0124 
0125 static const struct export_operations efs_export_ops = {
0126     .fh_to_dentry   = efs_fh_to_dentry,
0127     .fh_to_parent   = efs_fh_to_parent,
0128     .get_parent = efs_get_parent,
0129 };
0130 
0131 static int __init init_efs_fs(void) {
0132     int err;
0133     pr_info(EFS_VERSION" - http://aeschi.ch.eu.org/efs/\n");
0134     err = init_inodecache();
0135     if (err)
0136         goto out1;
0137     err = register_filesystem(&efs_fs_type);
0138     if (err)
0139         goto out;
0140     return 0;
0141 out:
0142     destroy_inodecache();
0143 out1:
0144     return err;
0145 }
0146 
0147 static void __exit exit_efs_fs(void) {
0148     unregister_filesystem(&efs_fs_type);
0149     destroy_inodecache();
0150 }
0151 
0152 module_init(init_efs_fs)
0153 module_exit(exit_efs_fs)
0154 
0155 static efs_block_t efs_validate_vh(struct volume_header *vh) {
0156     int     i;
0157     __be32      cs, *ui;
0158     int     csum;
0159     efs_block_t sblock = 0; /* shuts up gcc */
0160     struct pt_types *pt_entry;
0161     int     pt_type, slice = -1;
0162 
0163     if (be32_to_cpu(vh->vh_magic) != VHMAGIC) {
0164         /*
0165          * assume that we're dealing with a partition and allow
0166          * read_super() to try and detect a valid superblock
0167          * on the next block.
0168          */
0169         return 0;
0170     }
0171 
0172     ui = ((__be32 *) (vh + 1)) - 1;
0173     for(csum = 0; ui >= ((__be32 *) vh);) {
0174         cs = *ui--;
0175         csum += be32_to_cpu(cs);
0176     }
0177     if (csum) {
0178         pr_warn("SGI disklabel: checksum bad, label corrupted\n");
0179         return 0;
0180     }
0181 
0182 #ifdef DEBUG
0183     pr_debug("bf: \"%16s\"\n", vh->vh_bootfile);
0184 
0185     for(i = 0; i < NVDIR; i++) {
0186         int j;
0187         char    name[VDNAMESIZE+1];
0188 
0189         for(j = 0; j < VDNAMESIZE; j++) {
0190             name[j] = vh->vh_vd[i].vd_name[j];
0191         }
0192         name[j] = (char) 0;
0193 
0194         if (name[0]) {
0195             pr_debug("vh: %8s block: 0x%08x size: 0x%08x\n",
0196                 name, (int) be32_to_cpu(vh->vh_vd[i].vd_lbn),
0197                 (int) be32_to_cpu(vh->vh_vd[i].vd_nbytes));
0198         }
0199     }
0200 #endif
0201 
0202     for(i = 0; i < NPARTAB; i++) {
0203         pt_type = (int) be32_to_cpu(vh->vh_pt[i].pt_type);
0204         for(pt_entry = sgi_pt_types; pt_entry->pt_name; pt_entry++) {
0205             if (pt_type == pt_entry->pt_type) break;
0206         }
0207 #ifdef DEBUG
0208         if (be32_to_cpu(vh->vh_pt[i].pt_nblks)) {
0209             pr_debug("pt %2d: start: %08d size: %08d type: 0x%02x (%s)\n",
0210                  i, (int)be32_to_cpu(vh->vh_pt[i].pt_firstlbn),
0211                  (int)be32_to_cpu(vh->vh_pt[i].pt_nblks),
0212                  pt_type, (pt_entry->pt_name) ?
0213                  pt_entry->pt_name : "unknown");
0214         }
0215 #endif
0216         if (IS_EFS(pt_type)) {
0217             sblock = be32_to_cpu(vh->vh_pt[i].pt_firstlbn);
0218             slice = i;
0219         }
0220     }
0221 
0222     if (slice == -1) {
0223         pr_notice("partition table contained no EFS partitions\n");
0224 #ifdef DEBUG
0225     } else {
0226         pr_info("using slice %d (type %s, offset 0x%x)\n", slice,
0227             (pt_entry->pt_name) ? pt_entry->pt_name : "unknown",
0228             sblock);
0229 #endif
0230     }
0231     return sblock;
0232 }
0233 
0234 static int efs_validate_super(struct efs_sb_info *sb, struct efs_super *super) {
0235 
0236     if (!IS_EFS_MAGIC(be32_to_cpu(super->fs_magic)))
0237         return -1;
0238 
0239     sb->fs_magic     = be32_to_cpu(super->fs_magic);
0240     sb->total_blocks = be32_to_cpu(super->fs_size);
0241     sb->first_block  = be32_to_cpu(super->fs_firstcg);
0242     sb->group_size   = be32_to_cpu(super->fs_cgfsize);
0243     sb->data_free    = be32_to_cpu(super->fs_tfree);
0244     sb->inode_free   = be32_to_cpu(super->fs_tinode);
0245     sb->inode_blocks = be16_to_cpu(super->fs_cgisize);
0246     sb->total_groups = be16_to_cpu(super->fs_ncg);
0247     
0248     return 0;    
0249 }
0250 
0251 static int efs_fill_super(struct super_block *s, void *d, int silent)
0252 {
0253     struct efs_sb_info *sb;
0254     struct buffer_head *bh;
0255     struct inode *root;
0256 
0257     sb = kzalloc(sizeof(struct efs_sb_info), GFP_KERNEL);
0258     if (!sb)
0259         return -ENOMEM;
0260     s->s_fs_info = sb;
0261     s->s_time_min = 0;
0262     s->s_time_max = U32_MAX;
0263  
0264     s->s_magic      = EFS_SUPER_MAGIC;
0265     if (!sb_set_blocksize(s, EFS_BLOCKSIZE)) {
0266         pr_err("device does not support %d byte blocks\n",
0267             EFS_BLOCKSIZE);
0268         return -EINVAL;
0269     }
0270   
0271     /* read the vh (volume header) block */
0272     bh = sb_bread(s, 0);
0273 
0274     if (!bh) {
0275         pr_err("cannot read volume header\n");
0276         return -EIO;
0277     }
0278 
0279     /*
0280      * if this returns zero then we didn't find any partition table.
0281      * this isn't (yet) an error - just assume for the moment that
0282      * the device is valid and go on to search for a superblock.
0283      */
0284     sb->fs_start = efs_validate_vh((struct volume_header *) bh->b_data);
0285     brelse(bh);
0286 
0287     if (sb->fs_start == -1) {
0288         return -EINVAL;
0289     }
0290 
0291     bh = sb_bread(s, sb->fs_start + EFS_SUPER);
0292     if (!bh) {
0293         pr_err("cannot read superblock\n");
0294         return -EIO;
0295     }
0296         
0297     if (efs_validate_super(sb, (struct efs_super *) bh->b_data)) {
0298 #ifdef DEBUG
0299         pr_warn("invalid superblock at block %u\n",
0300             sb->fs_start + EFS_SUPER);
0301 #endif
0302         brelse(bh);
0303         return -EINVAL;
0304     }
0305     brelse(bh);
0306 
0307     if (!sb_rdonly(s)) {
0308 #ifdef DEBUG
0309         pr_info("forcing read-only mode\n");
0310 #endif
0311         s->s_flags |= SB_RDONLY;
0312     }
0313     s->s_op   = &efs_superblock_operations;
0314     s->s_export_op = &efs_export_ops;
0315     root = efs_iget(s, EFS_ROOTINODE);
0316     if (IS_ERR(root)) {
0317         pr_err("get root inode failed\n");
0318         return PTR_ERR(root);
0319     }
0320 
0321     s->s_root = d_make_root(root);
0322     if (!(s->s_root)) {
0323         pr_err("get root dentry failed\n");
0324         return -ENOMEM;
0325     }
0326 
0327     return 0;
0328 }
0329 
0330 static int efs_statfs(struct dentry *dentry, struct kstatfs *buf) {
0331     struct super_block *sb = dentry->d_sb;
0332     struct efs_sb_info *sbi = SUPER_INFO(sb);
0333     u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
0334 
0335     buf->f_type    = EFS_SUPER_MAGIC;   /* efs magic number */
0336     buf->f_bsize   = EFS_BLOCKSIZE;     /* blocksize */
0337     buf->f_blocks  = sbi->total_groups *    /* total data blocks */
0338             (sbi->group_size - sbi->inode_blocks);
0339     buf->f_bfree   = sbi->data_free;    /* free data blocks */
0340     buf->f_bavail  = sbi->data_free;    /* free blocks for non-root */
0341     buf->f_files   = sbi->total_groups *    /* total inodes */
0342             sbi->inode_blocks *
0343             (EFS_BLOCKSIZE / sizeof(struct efs_dinode));
0344     buf->f_ffree   = sbi->inode_free;   /* free inodes */
0345     buf->f_fsid    = u64_to_fsid(id);
0346     buf->f_namelen = EFS_MAXNAMELEN;    /* max filename length */
0347 
0348     return 0;
0349 }
0350