Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/fs/adfs/super.c
0004  *
0005  *  Copyright (C) 1997-1999 Russell King
0006  */
0007 #include <linux/module.h>
0008 #include <linux/init.h>
0009 #include <linux/parser.h>
0010 #include <linux/mount.h>
0011 #include <linux/seq_file.h>
0012 #include <linux/slab.h>
0013 #include <linux/statfs.h>
0014 #include <linux/user_namespace.h>
0015 #include <linux/blkdev.h>
0016 #include "adfs.h"
0017 #include "dir_f.h"
0018 #include "dir_fplus.h"
0019 
0020 #define ADFS_SB_FLAGS SB_NOATIME
0021 
0022 #define ADFS_DEFAULT_OWNER_MASK S_IRWXU
0023 #define ADFS_DEFAULT_OTHER_MASK (S_IRWXG | S_IRWXO)
0024 
0025 void __adfs_error(struct super_block *sb, const char *function, const char *fmt, ...)
0026 {
0027     struct va_format vaf;
0028     va_list args;
0029 
0030     va_start(args, fmt);
0031     vaf.fmt = fmt;
0032     vaf.va = &args;
0033 
0034     printk(KERN_CRIT "ADFS-fs error (device %s)%s%s: %pV\n",
0035         sb->s_id, function ? ": " : "",
0036         function ? function : "", &vaf);
0037 
0038     va_end(args);
0039 }
0040 
0041 void adfs_msg(struct super_block *sb, const char *pfx, const char *fmt, ...)
0042 {
0043     struct va_format vaf;
0044     va_list args;
0045 
0046     va_start(args, fmt);
0047     vaf.fmt = fmt;
0048     vaf.va = &args;
0049     printk("%sADFS-fs (%s): %pV\n", pfx, sb->s_id, &vaf);
0050     va_end(args);
0051 }
0052 
0053 static int adfs_checkdiscrecord(struct adfs_discrecord *dr)
0054 {
0055     unsigned int max_idlen;
0056     int i;
0057 
0058     /* sector size must be 256, 512 or 1024 bytes */
0059     if (dr->log2secsize != 8 &&
0060         dr->log2secsize != 9 &&
0061         dr->log2secsize != 10)
0062         return 1;
0063 
0064     /* idlen must be at least log2secsize + 3 */
0065     if (dr->idlen < dr->log2secsize + 3)
0066         return 1;
0067 
0068     /* we cannot have such a large disc that we
0069      * are unable to represent sector offsets in
0070      * 32 bits.  This works out at 2.0 TB.
0071      */
0072     if (le32_to_cpu(dr->disc_size_high) >> dr->log2secsize)
0073         return 1;
0074 
0075     /*
0076      * Maximum idlen is limited to 16 bits for new directories by
0077      * the three-byte storage of an indirect disc address.  For
0078      * big directories, idlen must be no greater than 19 v2 [1.0]
0079      */
0080     max_idlen = dr->format_version ? 19 : 16;
0081     if (dr->idlen > max_idlen)
0082         return 1;
0083 
0084     /* reserved bytes should be zero */
0085     for (i = 0; i < sizeof(dr->unused52); i++)
0086         if (dr->unused52[i] != 0)
0087             return 1;
0088 
0089     return 0;
0090 }
0091 
0092 static void adfs_put_super(struct super_block *sb)
0093 {
0094     struct adfs_sb_info *asb = ADFS_SB(sb);
0095 
0096     adfs_free_map(sb);
0097     kfree_rcu(asb, rcu);
0098 }
0099 
0100 static int adfs_show_options(struct seq_file *seq, struct dentry *root)
0101 {
0102     struct adfs_sb_info *asb = ADFS_SB(root->d_sb);
0103 
0104     if (!uid_eq(asb->s_uid, GLOBAL_ROOT_UID))
0105         seq_printf(seq, ",uid=%u", from_kuid_munged(&init_user_ns, asb->s_uid));
0106     if (!gid_eq(asb->s_gid, GLOBAL_ROOT_GID))
0107         seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, asb->s_gid));
0108     if (asb->s_owner_mask != ADFS_DEFAULT_OWNER_MASK)
0109         seq_printf(seq, ",ownmask=%o", asb->s_owner_mask);
0110     if (asb->s_other_mask != ADFS_DEFAULT_OTHER_MASK)
0111         seq_printf(seq, ",othmask=%o", asb->s_other_mask);
0112     if (asb->s_ftsuffix != 0)
0113         seq_printf(seq, ",ftsuffix=%u", asb->s_ftsuffix);
0114 
0115     return 0;
0116 }
0117 
0118 enum {Opt_uid, Opt_gid, Opt_ownmask, Opt_othmask, Opt_ftsuffix, Opt_err};
0119 
0120 static const match_table_t tokens = {
0121     {Opt_uid, "uid=%u"},
0122     {Opt_gid, "gid=%u"},
0123     {Opt_ownmask, "ownmask=%o"},
0124     {Opt_othmask, "othmask=%o"},
0125     {Opt_ftsuffix, "ftsuffix=%u"},
0126     {Opt_err, NULL}
0127 };
0128 
0129 static int parse_options(struct super_block *sb, struct adfs_sb_info *asb,
0130              char *options)
0131 {
0132     char *p;
0133     int option;
0134 
0135     if (!options)
0136         return 0;
0137 
0138     while ((p = strsep(&options, ",")) != NULL) {
0139         substring_t args[MAX_OPT_ARGS];
0140         int token;
0141         if (!*p)
0142             continue;
0143 
0144         token = match_token(p, tokens, args);
0145         switch (token) {
0146         case Opt_uid:
0147             if (match_int(args, &option))
0148                 return -EINVAL;
0149             asb->s_uid = make_kuid(current_user_ns(), option);
0150             if (!uid_valid(asb->s_uid))
0151                 return -EINVAL;
0152             break;
0153         case Opt_gid:
0154             if (match_int(args, &option))
0155                 return -EINVAL;
0156             asb->s_gid = make_kgid(current_user_ns(), option);
0157             if (!gid_valid(asb->s_gid))
0158                 return -EINVAL;
0159             break;
0160         case Opt_ownmask:
0161             if (match_octal(args, &option))
0162                 return -EINVAL;
0163             asb->s_owner_mask = option;
0164             break;
0165         case Opt_othmask:
0166             if (match_octal(args, &option))
0167                 return -EINVAL;
0168             asb->s_other_mask = option;
0169             break;
0170         case Opt_ftsuffix:
0171             if (match_int(args, &option))
0172                 return -EINVAL;
0173             asb->s_ftsuffix = option;
0174             break;
0175         default:
0176             adfs_msg(sb, KERN_ERR,
0177                  "unrecognised mount option \"%s\" or missing value",
0178                  p);
0179             return -EINVAL;
0180         }
0181     }
0182     return 0;
0183 }
0184 
0185 static int adfs_remount(struct super_block *sb, int *flags, char *data)
0186 {
0187     struct adfs_sb_info temp_asb;
0188     int ret;
0189 
0190     sync_filesystem(sb);
0191     *flags |= ADFS_SB_FLAGS;
0192 
0193     temp_asb = *ADFS_SB(sb);
0194     ret = parse_options(sb, &temp_asb, data);
0195     if (ret == 0)
0196         *ADFS_SB(sb) = temp_asb;
0197 
0198     return ret;
0199 }
0200 
0201 static int adfs_statfs(struct dentry *dentry, struct kstatfs *buf)
0202 {
0203     struct super_block *sb = dentry->d_sb;
0204     struct adfs_sb_info *sbi = ADFS_SB(sb);
0205     u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
0206 
0207     adfs_map_statfs(sb, buf);
0208 
0209     buf->f_type    = ADFS_SUPER_MAGIC;
0210     buf->f_namelen = sbi->s_namelen;
0211     buf->f_bsize   = sb->s_blocksize;
0212     buf->f_ffree   = (long)(buf->f_bfree * buf->f_files) / (long)buf->f_blocks;
0213     buf->f_fsid    = u64_to_fsid(id);
0214 
0215     return 0;
0216 }
0217 
0218 static struct kmem_cache *adfs_inode_cachep;
0219 
0220 static struct inode *adfs_alloc_inode(struct super_block *sb)
0221 {
0222     struct adfs_inode_info *ei;
0223     ei = alloc_inode_sb(sb, adfs_inode_cachep, GFP_KERNEL);
0224     if (!ei)
0225         return NULL;
0226     return &ei->vfs_inode;
0227 }
0228 
0229 static void adfs_free_inode(struct inode *inode)
0230 {
0231     kmem_cache_free(adfs_inode_cachep, ADFS_I(inode));
0232 }
0233 
0234 static int adfs_drop_inode(struct inode *inode)
0235 {
0236     /* always drop inodes if we are read-only */
0237     return !IS_ENABLED(CONFIG_ADFS_FS_RW) || IS_RDONLY(inode);
0238 }
0239 
0240 static void init_once(void *foo)
0241 {
0242     struct adfs_inode_info *ei = (struct adfs_inode_info *) foo;
0243 
0244     inode_init_once(&ei->vfs_inode);
0245 }
0246 
0247 static int __init init_inodecache(void)
0248 {
0249     adfs_inode_cachep = kmem_cache_create("adfs_inode_cache",
0250                          sizeof(struct adfs_inode_info),
0251                          0, (SLAB_RECLAIM_ACCOUNT|
0252                         SLAB_MEM_SPREAD|SLAB_ACCOUNT),
0253                          init_once);
0254     if (adfs_inode_cachep == NULL)
0255         return -ENOMEM;
0256     return 0;
0257 }
0258 
0259 static void destroy_inodecache(void)
0260 {
0261     /*
0262      * Make sure all delayed rcu free inodes are flushed before we
0263      * destroy cache.
0264      */
0265     rcu_barrier();
0266     kmem_cache_destroy(adfs_inode_cachep);
0267 }
0268 
0269 static const struct super_operations adfs_sops = {
0270     .alloc_inode    = adfs_alloc_inode,
0271     .free_inode = adfs_free_inode,
0272     .drop_inode = adfs_drop_inode,
0273     .write_inode    = adfs_write_inode,
0274     .put_super  = adfs_put_super,
0275     .statfs     = adfs_statfs,
0276     .remount_fs = adfs_remount,
0277     .show_options   = adfs_show_options,
0278 };
0279 
0280 static int adfs_probe(struct super_block *sb, unsigned int offset, int silent,
0281               int (*validate)(struct super_block *sb,
0282                       struct buffer_head *bh,
0283                       struct adfs_discrecord **bhp))
0284 {
0285     struct adfs_sb_info *asb = ADFS_SB(sb);
0286     struct adfs_discrecord *dr;
0287     struct buffer_head *bh;
0288     unsigned int blocksize = BLOCK_SIZE;
0289     int ret, try;
0290 
0291     for (try = 0; try < 2; try++) {
0292         /* try to set the requested block size */
0293         if (sb->s_blocksize != blocksize &&
0294             !sb_set_blocksize(sb, blocksize)) {
0295             if (!silent)
0296                 adfs_msg(sb, KERN_ERR,
0297                      "error: unsupported blocksize");
0298             return -EINVAL;
0299         }
0300 
0301         /* read the buffer */
0302         bh = sb_bread(sb, offset >> sb->s_blocksize_bits);
0303         if (!bh) {
0304             adfs_msg(sb, KERN_ERR,
0305                  "error: unable to read block %u, try %d",
0306                  offset >> sb->s_blocksize_bits, try);
0307             return -EIO;
0308         }
0309 
0310         /* validate it */
0311         ret = validate(sb, bh, &dr);
0312         if (ret) {
0313             brelse(bh);
0314             return ret;
0315         }
0316 
0317         /* does the block size match the filesystem block size? */
0318         blocksize = 1 << dr->log2secsize;
0319         if (sb->s_blocksize == blocksize) {
0320             asb->s_map = adfs_read_map(sb, dr);
0321             brelse(bh);
0322             return PTR_ERR_OR_ZERO(asb->s_map);
0323         }
0324 
0325         brelse(bh);
0326     }
0327 
0328     return -EIO;
0329 }
0330 
0331 static int adfs_validate_bblk(struct super_block *sb, struct buffer_head *bh,
0332                   struct adfs_discrecord **drp)
0333 {
0334     struct adfs_discrecord *dr;
0335     unsigned char *b_data;
0336 
0337     b_data = bh->b_data + (ADFS_DISCRECORD % sb->s_blocksize);
0338     if (adfs_checkbblk(b_data))
0339         return -EILSEQ;
0340 
0341     /* Do some sanity checks on the ADFS disc record */
0342     dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
0343     if (adfs_checkdiscrecord(dr))
0344         return -EILSEQ;
0345 
0346     *drp = dr;
0347     return 0;
0348 }
0349 
0350 static int adfs_validate_dr0(struct super_block *sb, struct buffer_head *bh,
0351                   struct adfs_discrecord **drp)
0352 {
0353     struct adfs_discrecord *dr;
0354 
0355     /* Do some sanity checks on the ADFS disc record */
0356     dr = (struct adfs_discrecord *)(bh->b_data + 4);
0357     if (adfs_checkdiscrecord(dr) || dr->nzones_high || dr->nzones != 1)
0358         return -EILSEQ;
0359 
0360     *drp = dr;
0361     return 0;
0362 }
0363 
0364 static int adfs_fill_super(struct super_block *sb, void *data, int silent)
0365 {
0366     struct adfs_discrecord *dr;
0367     struct object_info root_obj;
0368     struct adfs_sb_info *asb;
0369     struct inode *root;
0370     int ret = -EINVAL;
0371 
0372     sb->s_flags |= ADFS_SB_FLAGS;
0373 
0374     asb = kzalloc(sizeof(*asb), GFP_KERNEL);
0375     if (!asb)
0376         return -ENOMEM;
0377 
0378     sb->s_fs_info = asb;
0379     sb->s_magic = ADFS_SUPER_MAGIC;
0380     sb->s_time_gran = 10000000;
0381 
0382     /* set default options */
0383     asb->s_uid = GLOBAL_ROOT_UID;
0384     asb->s_gid = GLOBAL_ROOT_GID;
0385     asb->s_owner_mask = ADFS_DEFAULT_OWNER_MASK;
0386     asb->s_other_mask = ADFS_DEFAULT_OTHER_MASK;
0387     asb->s_ftsuffix = 0;
0388 
0389     if (parse_options(sb, asb, data))
0390         goto error;
0391 
0392     /* Try to probe the filesystem boot block */
0393     ret = adfs_probe(sb, ADFS_DISCRECORD, 1, adfs_validate_bblk);
0394     if (ret == -EILSEQ)
0395         ret = adfs_probe(sb, 0, silent, adfs_validate_dr0);
0396     if (ret == -EILSEQ) {
0397         if (!silent)
0398             adfs_msg(sb, KERN_ERR,
0399                  "error: can't find an ADFS filesystem on dev %s.",
0400                  sb->s_id);
0401         ret = -EINVAL;
0402     }
0403     if (ret)
0404         goto error;
0405 
0406     /* set up enough so that we can read an inode */
0407     sb->s_op = &adfs_sops;
0408 
0409     dr = adfs_map_discrecord(asb->s_map);
0410 
0411     root_obj.parent_id = root_obj.indaddr = le32_to_cpu(dr->root);
0412     root_obj.name_len  = 0;
0413     /* Set root object date as 01 Jan 1987 00:00:00 */
0414     root_obj.loadaddr  = 0xfff0003f;
0415     root_obj.execaddr  = 0xec22c000;
0416     root_obj.size      = ADFS_NEWDIR_SIZE;
0417     root_obj.attr      = ADFS_NDA_DIRECTORY   | ADFS_NDA_OWNER_READ |
0418                  ADFS_NDA_OWNER_WRITE | ADFS_NDA_PUBLIC_READ;
0419 
0420     /*
0421      * If this is a F+ disk with variable length directories,
0422      * get the root_size from the disc record.
0423      */
0424     if (dr->format_version) {
0425         root_obj.size = le32_to_cpu(dr->root_size);
0426         asb->s_dir     = &adfs_fplus_dir_ops;
0427         asb->s_namelen = ADFS_FPLUS_NAME_LEN;
0428     } else {
0429         asb->s_dir     = &adfs_f_dir_ops;
0430         asb->s_namelen = ADFS_F_NAME_LEN;
0431     }
0432     /*
0433      * ,xyz hex filetype suffix may be added by driver
0434      * to files that have valid RISC OS filetype
0435      */
0436     if (asb->s_ftsuffix)
0437         asb->s_namelen += 4;
0438 
0439     sb->s_d_op = &adfs_dentry_operations;
0440     root = adfs_iget(sb, &root_obj);
0441     sb->s_root = d_make_root(root);
0442     if (!sb->s_root) {
0443         adfs_free_map(sb);
0444         adfs_error(sb, "get root inode failed\n");
0445         ret = -EIO;
0446         goto error;
0447     }
0448     return 0;
0449 
0450 error:
0451     sb->s_fs_info = NULL;
0452     kfree(asb);
0453     return ret;
0454 }
0455 
0456 static struct dentry *adfs_mount(struct file_system_type *fs_type,
0457     int flags, const char *dev_name, void *data)
0458 {
0459     return mount_bdev(fs_type, flags, dev_name, data, adfs_fill_super);
0460 }
0461 
0462 static struct file_system_type adfs_fs_type = {
0463     .owner      = THIS_MODULE,
0464     .name       = "adfs",
0465     .mount      = adfs_mount,
0466     .kill_sb    = kill_block_super,
0467     .fs_flags   = FS_REQUIRES_DEV,
0468 };
0469 MODULE_ALIAS_FS("adfs");
0470 
0471 static int __init init_adfs_fs(void)
0472 {
0473     int err = init_inodecache();
0474     if (err)
0475         goto out1;
0476     err = register_filesystem(&adfs_fs_type);
0477     if (err)
0478         goto out;
0479     return 0;
0480 out:
0481     destroy_inodecache();
0482 out1:
0483     return err;
0484 }
0485 
0486 static void __exit exit_adfs_fs(void)
0487 {
0488     unregister_filesystem(&adfs_fs_type);
0489     destroy_inodecache();
0490 }
0491 
0492 module_init(init_adfs_fs)
0493 module_exit(exit_adfs_fs)
0494 MODULE_LICENSE("GPL");