Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Optimized MPEG FS - inode and super operations.
0004  * Copyright (C) 2006 Bob Copeland <me@bobcopeland.com>
0005  */
0006 #include <linux/module.h>
0007 #include <linux/sched.h>
0008 #include <linux/slab.h>
0009 #include <linux/fs.h>
0010 #include <linux/vfs.h>
0011 #include <linux/cred.h>
0012 #include <linux/parser.h>
0013 #include <linux/buffer_head.h>
0014 #include <linux/vmalloc.h>
0015 #include <linux/writeback.h>
0016 #include <linux/seq_file.h>
0017 #include <linux/crc-itu-t.h>
0018 #include "omfs.h"
0019 
0020 MODULE_AUTHOR("Bob Copeland <me@bobcopeland.com>");
0021 MODULE_DESCRIPTION("OMFS (ReplayTV/Karma) Filesystem for Linux");
0022 MODULE_LICENSE("GPL");
0023 
0024 struct buffer_head *omfs_bread(struct super_block *sb, sector_t block)
0025 {
0026     struct omfs_sb_info *sbi = OMFS_SB(sb);
0027     if (block >= sbi->s_num_blocks)
0028         return NULL;
0029 
0030     return sb_bread(sb, clus_to_blk(sbi, block));
0031 }
0032 
0033 struct inode *omfs_new_inode(struct inode *dir, umode_t mode)
0034 {
0035     struct inode *inode;
0036     u64 new_block;
0037     int err;
0038     int len;
0039     struct omfs_sb_info *sbi = OMFS_SB(dir->i_sb);
0040 
0041     inode = new_inode(dir->i_sb);
0042     if (!inode)
0043         return ERR_PTR(-ENOMEM);
0044 
0045     err = omfs_allocate_range(dir->i_sb, sbi->s_mirrors, sbi->s_mirrors,
0046             &new_block, &len);
0047     if (err)
0048         goto fail;
0049 
0050     inode->i_ino = new_block;
0051     inode_init_owner(&init_user_ns, inode, NULL, mode);
0052     inode->i_mapping->a_ops = &omfs_aops;
0053 
0054     inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0055     switch (mode & S_IFMT) {
0056     case S_IFDIR:
0057         inode->i_op = &omfs_dir_inops;
0058         inode->i_fop = &omfs_dir_operations;
0059         inode->i_size = sbi->s_sys_blocksize;
0060         inc_nlink(inode);
0061         break;
0062     case S_IFREG:
0063         inode->i_op = &omfs_file_inops;
0064         inode->i_fop = &omfs_file_operations;
0065         inode->i_size = 0;
0066         break;
0067     }
0068 
0069     insert_inode_hash(inode);
0070     mark_inode_dirty(inode);
0071     return inode;
0072 fail:
0073     make_bad_inode(inode);
0074     iput(inode);
0075     return ERR_PTR(err);
0076 }
0077 
0078 /*
0079  * Update the header checksums for a dirty inode based on its contents.
0080  * Caller is expected to hold the buffer head underlying oi and mark it
0081  * dirty.
0082  */
0083 static void omfs_update_checksums(struct omfs_inode *oi)
0084 {
0085     int xor, i, ofs = 0, count;
0086     u16 crc = 0;
0087     unsigned char *ptr = (unsigned char *) oi;
0088 
0089     count = be32_to_cpu(oi->i_head.h_body_size);
0090     ofs = sizeof(struct omfs_header);
0091 
0092     crc = crc_itu_t(crc, ptr + ofs, count);
0093     oi->i_head.h_crc = cpu_to_be16(crc);
0094 
0095     xor = ptr[0];
0096     for (i = 1; i < OMFS_XOR_COUNT; i++)
0097         xor ^= ptr[i];
0098 
0099     oi->i_head.h_check_xor = xor;
0100 }
0101 
0102 static int __omfs_write_inode(struct inode *inode, int wait)
0103 {
0104     struct omfs_inode *oi;
0105     struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
0106     struct buffer_head *bh, *bh2;
0107     u64 ctime;
0108     int i;
0109     int ret = -EIO;
0110     int sync_failed = 0;
0111 
0112     /* get current inode since we may have written sibling ptrs etc. */
0113     bh = omfs_bread(inode->i_sb, inode->i_ino);
0114     if (!bh)
0115         goto out;
0116 
0117     oi = (struct omfs_inode *) bh->b_data;
0118 
0119     oi->i_head.h_self = cpu_to_be64(inode->i_ino);
0120     if (S_ISDIR(inode->i_mode))
0121         oi->i_type = OMFS_DIR;
0122     else if (S_ISREG(inode->i_mode))
0123         oi->i_type = OMFS_FILE;
0124     else {
0125         printk(KERN_WARNING "omfs: unknown file type: %d\n",
0126             inode->i_mode);
0127         goto out_brelse;
0128     }
0129 
0130     oi->i_head.h_body_size = cpu_to_be32(sbi->s_sys_blocksize -
0131         sizeof(struct omfs_header));
0132     oi->i_head.h_version = 1;
0133     oi->i_head.h_type = OMFS_INODE_NORMAL;
0134     oi->i_head.h_magic = OMFS_IMAGIC;
0135     oi->i_size = cpu_to_be64(inode->i_size);
0136 
0137     ctime = inode->i_ctime.tv_sec * 1000LL +
0138         ((inode->i_ctime.tv_nsec + 999)/1000);
0139     oi->i_ctime = cpu_to_be64(ctime);
0140 
0141     omfs_update_checksums(oi);
0142 
0143     mark_buffer_dirty(bh);
0144     if (wait) {
0145         sync_dirty_buffer(bh);
0146         if (buffer_req(bh) && !buffer_uptodate(bh))
0147             sync_failed = 1;
0148     }
0149 
0150     /* if mirroring writes, copy to next fsblock */
0151     for (i = 1; i < sbi->s_mirrors; i++) {
0152         bh2 = omfs_bread(inode->i_sb, inode->i_ino + i);
0153         if (!bh2)
0154             goto out_brelse;
0155 
0156         memcpy(bh2->b_data, bh->b_data, bh->b_size);
0157         mark_buffer_dirty(bh2);
0158         if (wait) {
0159             sync_dirty_buffer(bh2);
0160             if (buffer_req(bh2) && !buffer_uptodate(bh2))
0161                 sync_failed = 1;
0162         }
0163         brelse(bh2);
0164     }
0165     ret = (sync_failed) ? -EIO : 0;
0166 out_brelse:
0167     brelse(bh);
0168 out:
0169     return ret;
0170 }
0171 
0172 static int omfs_write_inode(struct inode *inode, struct writeback_control *wbc)
0173 {
0174     return __omfs_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
0175 }
0176 
0177 int omfs_sync_inode(struct inode *inode)
0178 {
0179     return __omfs_write_inode(inode, 1);
0180 }
0181 
0182 /*
0183  * called when an entry is deleted, need to clear the bits in the
0184  * bitmaps.
0185  */
0186 static void omfs_evict_inode(struct inode *inode)
0187 {
0188     truncate_inode_pages_final(&inode->i_data);
0189     clear_inode(inode);
0190 
0191     if (inode->i_nlink)
0192         return;
0193 
0194     if (S_ISREG(inode->i_mode)) {
0195         inode->i_size = 0;
0196         omfs_shrink_inode(inode);
0197     }
0198 
0199     omfs_clear_range(inode->i_sb, inode->i_ino, 2);
0200 }
0201 
0202 struct inode *omfs_iget(struct super_block *sb, ino_t ino)
0203 {
0204     struct omfs_sb_info *sbi = OMFS_SB(sb);
0205     struct omfs_inode *oi;
0206     struct buffer_head *bh;
0207     u64 ctime;
0208     unsigned long nsecs;
0209     struct inode *inode;
0210 
0211     inode = iget_locked(sb, ino);
0212     if (!inode)
0213         return ERR_PTR(-ENOMEM);
0214     if (!(inode->i_state & I_NEW))
0215         return inode;
0216 
0217     bh = omfs_bread(inode->i_sb, ino);
0218     if (!bh)
0219         goto iget_failed;
0220 
0221     oi = (struct omfs_inode *)bh->b_data;
0222 
0223     /* check self */
0224     if (ino != be64_to_cpu(oi->i_head.h_self))
0225         goto fail_bh;
0226 
0227     inode->i_uid = sbi->s_uid;
0228     inode->i_gid = sbi->s_gid;
0229 
0230     ctime = be64_to_cpu(oi->i_ctime);
0231     nsecs = do_div(ctime, 1000) * 1000L;
0232 
0233     inode->i_atime.tv_sec = ctime;
0234     inode->i_mtime.tv_sec = ctime;
0235     inode->i_ctime.tv_sec = ctime;
0236     inode->i_atime.tv_nsec = nsecs;
0237     inode->i_mtime.tv_nsec = nsecs;
0238     inode->i_ctime.tv_nsec = nsecs;
0239 
0240     inode->i_mapping->a_ops = &omfs_aops;
0241 
0242     switch (oi->i_type) {
0243     case OMFS_DIR:
0244         inode->i_mode = S_IFDIR | (S_IRWXUGO & ~sbi->s_dmask);
0245         inode->i_op = &omfs_dir_inops;
0246         inode->i_fop = &omfs_dir_operations;
0247         inode->i_size = sbi->s_sys_blocksize;
0248         inc_nlink(inode);
0249         break;
0250     case OMFS_FILE:
0251         inode->i_mode = S_IFREG | (S_IRWXUGO & ~sbi->s_fmask);
0252         inode->i_fop = &omfs_file_operations;
0253         inode->i_size = be64_to_cpu(oi->i_size);
0254         break;
0255     }
0256     brelse(bh);
0257     unlock_new_inode(inode);
0258     return inode;
0259 fail_bh:
0260     brelse(bh);
0261 iget_failed:
0262     iget_failed(inode);
0263     return ERR_PTR(-EIO);
0264 }
0265 
0266 static void omfs_put_super(struct super_block *sb)
0267 {
0268     struct omfs_sb_info *sbi = OMFS_SB(sb);
0269     kfree(sbi->s_imap);
0270     kfree(sbi);
0271     sb->s_fs_info = NULL;
0272 }
0273 
0274 static int omfs_statfs(struct dentry *dentry, struct kstatfs *buf)
0275 {
0276     struct super_block *s = dentry->d_sb;
0277     struct omfs_sb_info *sbi = OMFS_SB(s);
0278     u64 id = huge_encode_dev(s->s_bdev->bd_dev);
0279 
0280     buf->f_type = OMFS_MAGIC;
0281     buf->f_bsize = sbi->s_blocksize;
0282     buf->f_blocks = sbi->s_num_blocks;
0283     buf->f_files = sbi->s_num_blocks;
0284     buf->f_namelen = OMFS_NAMELEN;
0285     buf->f_fsid = u64_to_fsid(id);
0286 
0287     buf->f_bfree = buf->f_bavail = buf->f_ffree =
0288         omfs_count_free(s);
0289 
0290     return 0;
0291 }
0292 
0293 /*
0294  * Display the mount options in /proc/mounts.
0295  */
0296 static int omfs_show_options(struct seq_file *m, struct dentry *root)
0297 {
0298     struct omfs_sb_info *sbi = OMFS_SB(root->d_sb);
0299     umode_t cur_umask = current_umask();
0300 
0301     if (!uid_eq(sbi->s_uid, current_uid()))
0302         seq_printf(m, ",uid=%u",
0303                from_kuid_munged(&init_user_ns, sbi->s_uid));
0304     if (!gid_eq(sbi->s_gid, current_gid()))
0305         seq_printf(m, ",gid=%u",
0306                from_kgid_munged(&init_user_ns, sbi->s_gid));
0307 
0308     if (sbi->s_dmask == sbi->s_fmask) {
0309         if (sbi->s_fmask != cur_umask)
0310             seq_printf(m, ",umask=%o", sbi->s_fmask);
0311     } else {
0312         if (sbi->s_dmask != cur_umask)
0313             seq_printf(m, ",dmask=%o", sbi->s_dmask);
0314         if (sbi->s_fmask != cur_umask)
0315             seq_printf(m, ",fmask=%o", sbi->s_fmask);
0316     }
0317 
0318     return 0;
0319 }
0320 
0321 static const struct super_operations omfs_sops = {
0322     .write_inode    = omfs_write_inode,
0323     .evict_inode    = omfs_evict_inode,
0324     .put_super  = omfs_put_super,
0325     .statfs     = omfs_statfs,
0326     .show_options   = omfs_show_options,
0327 };
0328 
0329 /*
0330  * For Rio Karma, there is an on-disk free bitmap whose location is
0331  * stored in the root block.  For ReplayTV, there is no such free bitmap
0332  * so we have to walk the tree.  Both inodes and file data are allocated
0333  * from the same map.  This array can be big (300k) so we allocate
0334  * in units of the blocksize.
0335  */
0336 static int omfs_get_imap(struct super_block *sb)
0337 {
0338     unsigned int bitmap_size, array_size;
0339     int count;
0340     struct omfs_sb_info *sbi = OMFS_SB(sb);
0341     struct buffer_head *bh;
0342     unsigned long **ptr;
0343     sector_t block;
0344 
0345     bitmap_size = DIV_ROUND_UP(sbi->s_num_blocks, 8);
0346     array_size = DIV_ROUND_UP(bitmap_size, sb->s_blocksize);
0347 
0348     if (sbi->s_bitmap_ino == ~0ULL)
0349         goto out;
0350 
0351     sbi->s_imap_size = array_size;
0352     sbi->s_imap = kcalloc(array_size, sizeof(unsigned long *), GFP_KERNEL);
0353     if (!sbi->s_imap)
0354         goto nomem;
0355 
0356     block = clus_to_blk(sbi, sbi->s_bitmap_ino);
0357     if (block >= sbi->s_num_blocks)
0358         goto nomem;
0359 
0360     ptr = sbi->s_imap;
0361     for (count = bitmap_size; count > 0; count -= sb->s_blocksize) {
0362         bh = sb_bread(sb, block++);
0363         if (!bh)
0364             goto nomem_free;
0365         *ptr = kmemdup(bh->b_data, sb->s_blocksize, GFP_KERNEL);
0366         if (!*ptr) {
0367             brelse(bh);
0368             goto nomem_free;
0369         }
0370         if (count < sb->s_blocksize)
0371             memset((void *)*ptr + count, 0xff,
0372                 sb->s_blocksize - count);
0373         brelse(bh);
0374         ptr++;
0375     }
0376 out:
0377     return 0;
0378 
0379 nomem_free:
0380     for (count = 0; count < array_size; count++)
0381         kfree(sbi->s_imap[count]);
0382 
0383     kfree(sbi->s_imap);
0384 nomem:
0385     sbi->s_imap = NULL;
0386     sbi->s_imap_size = 0;
0387     return -ENOMEM;
0388 }
0389 
0390 enum {
0391     Opt_uid, Opt_gid, Opt_umask, Opt_dmask, Opt_fmask, Opt_err
0392 };
0393 
0394 static const match_table_t tokens = {
0395     {Opt_uid, "uid=%u"},
0396     {Opt_gid, "gid=%u"},
0397     {Opt_umask, "umask=%o"},
0398     {Opt_dmask, "dmask=%o"},
0399     {Opt_fmask, "fmask=%o"},
0400     {Opt_err, NULL},
0401 };
0402 
0403 static int parse_options(char *options, struct omfs_sb_info *sbi)
0404 {
0405     char *p;
0406     substring_t args[MAX_OPT_ARGS];
0407     int option;
0408 
0409     if (!options)
0410         return 1;
0411 
0412     while ((p = strsep(&options, ",")) != NULL) {
0413         int token;
0414         if (!*p)
0415             continue;
0416 
0417         token = match_token(p, tokens, args);
0418         switch (token) {
0419         case Opt_uid:
0420             if (match_int(&args[0], &option))
0421                 return 0;
0422             sbi->s_uid = make_kuid(current_user_ns(), option);
0423             if (!uid_valid(sbi->s_uid))
0424                 return 0;
0425             break;
0426         case Opt_gid:
0427             if (match_int(&args[0], &option))
0428                 return 0;
0429             sbi->s_gid = make_kgid(current_user_ns(), option);
0430             if (!gid_valid(sbi->s_gid))
0431                 return 0;
0432             break;
0433         case Opt_umask:
0434             if (match_octal(&args[0], &option))
0435                 return 0;
0436             sbi->s_fmask = sbi->s_dmask = option;
0437             break;
0438         case Opt_dmask:
0439             if (match_octal(&args[0], &option))
0440                 return 0;
0441             sbi->s_dmask = option;
0442             break;
0443         case Opt_fmask:
0444             if (match_octal(&args[0], &option))
0445                 return 0;
0446             sbi->s_fmask = option;
0447             break;
0448         default:
0449             return 0;
0450         }
0451     }
0452     return 1;
0453 }
0454 
0455 static int omfs_fill_super(struct super_block *sb, void *data, int silent)
0456 {
0457     struct buffer_head *bh, *bh2;
0458     struct omfs_super_block *omfs_sb;
0459     struct omfs_root_block *omfs_rb;
0460     struct omfs_sb_info *sbi;
0461     struct inode *root;
0462     int ret = -EINVAL;
0463 
0464     sbi = kzalloc(sizeof(struct omfs_sb_info), GFP_KERNEL);
0465     if (!sbi)
0466         return -ENOMEM;
0467 
0468     sb->s_fs_info = sbi;
0469 
0470     sbi->s_uid = current_uid();
0471     sbi->s_gid = current_gid();
0472     sbi->s_dmask = sbi->s_fmask = current_umask();
0473 
0474     if (!parse_options((char *) data, sbi))
0475         goto end;
0476 
0477     sb->s_maxbytes = 0xffffffff;
0478 
0479     sb->s_time_gran = NSEC_PER_MSEC;
0480     sb->s_time_min = 0;
0481     sb->s_time_max = U64_MAX / MSEC_PER_SEC;
0482 
0483     sb_set_blocksize(sb, 0x200);
0484 
0485     bh = sb_bread(sb, 0);
0486     if (!bh)
0487         goto end;
0488 
0489     omfs_sb = (struct omfs_super_block *)bh->b_data;
0490 
0491     if (omfs_sb->s_magic != cpu_to_be32(OMFS_MAGIC)) {
0492         if (!silent)
0493             printk(KERN_ERR "omfs: Invalid superblock (%x)\n",
0494                    omfs_sb->s_magic);
0495         goto out_brelse_bh;
0496     }
0497     sb->s_magic = OMFS_MAGIC;
0498 
0499     sbi->s_num_blocks = be64_to_cpu(omfs_sb->s_num_blocks);
0500     sbi->s_blocksize = be32_to_cpu(omfs_sb->s_blocksize);
0501     sbi->s_mirrors = be32_to_cpu(omfs_sb->s_mirrors);
0502     sbi->s_root_ino = be64_to_cpu(omfs_sb->s_root_block);
0503     sbi->s_sys_blocksize = be32_to_cpu(omfs_sb->s_sys_blocksize);
0504     mutex_init(&sbi->s_bitmap_lock);
0505 
0506     if (sbi->s_num_blocks > OMFS_MAX_BLOCKS) {
0507         printk(KERN_ERR "omfs: sysblock number (%llx) is out of range\n",
0508                (unsigned long long)sbi->s_num_blocks);
0509         goto out_brelse_bh;
0510     }
0511 
0512     if (sbi->s_sys_blocksize > PAGE_SIZE) {
0513         printk(KERN_ERR "omfs: sysblock size (%d) is out of range\n",
0514             sbi->s_sys_blocksize);
0515         goto out_brelse_bh;
0516     }
0517 
0518     if (sbi->s_blocksize < sbi->s_sys_blocksize ||
0519         sbi->s_blocksize > OMFS_MAX_BLOCK_SIZE) {
0520         printk(KERN_ERR "omfs: block size (%d) is out of range\n",
0521             sbi->s_blocksize);
0522         goto out_brelse_bh;
0523     }
0524 
0525     /*
0526      * Use sys_blocksize as the fs block since it is smaller than a
0527      * page while the fs blocksize can be larger.
0528      */
0529     sb_set_blocksize(sb, sbi->s_sys_blocksize);
0530 
0531     /*
0532      * ...and the difference goes into a shift.  sys_blocksize is always
0533      * a power of two factor of blocksize.
0534      */
0535     sbi->s_block_shift = get_bitmask_order(sbi->s_blocksize) -
0536         get_bitmask_order(sbi->s_sys_blocksize);
0537 
0538     bh2 = omfs_bread(sb, be64_to_cpu(omfs_sb->s_root_block));
0539     if (!bh2)
0540         goto out_brelse_bh;
0541 
0542     omfs_rb = (struct omfs_root_block *)bh2->b_data;
0543 
0544     sbi->s_bitmap_ino = be64_to_cpu(omfs_rb->r_bitmap);
0545     sbi->s_clustersize = be32_to_cpu(omfs_rb->r_clustersize);
0546 
0547     if (sbi->s_num_blocks != be64_to_cpu(omfs_rb->r_num_blocks)) {
0548         printk(KERN_ERR "omfs: block count discrepancy between "
0549             "super and root blocks (%llx, %llx)\n",
0550             (unsigned long long)sbi->s_num_blocks,
0551             (unsigned long long)be64_to_cpu(omfs_rb->r_num_blocks));
0552         goto out_brelse_bh2;
0553     }
0554 
0555     if (sbi->s_bitmap_ino != ~0ULL &&
0556         sbi->s_bitmap_ino > sbi->s_num_blocks) {
0557         printk(KERN_ERR "omfs: free space bitmap location is corrupt "
0558             "(%llx, total blocks %llx)\n",
0559             (unsigned long long) sbi->s_bitmap_ino,
0560             (unsigned long long) sbi->s_num_blocks);
0561         goto out_brelse_bh2;
0562     }
0563     if (sbi->s_clustersize < 1 ||
0564         sbi->s_clustersize > OMFS_MAX_CLUSTER_SIZE) {
0565         printk(KERN_ERR "omfs: cluster size out of range (%d)",
0566             sbi->s_clustersize);
0567         goto out_brelse_bh2;
0568     }
0569 
0570     ret = omfs_get_imap(sb);
0571     if (ret)
0572         goto out_brelse_bh2;
0573 
0574     sb->s_op = &omfs_sops;
0575 
0576     root = omfs_iget(sb, be64_to_cpu(omfs_rb->r_root_dir));
0577     if (IS_ERR(root)) {
0578         ret = PTR_ERR(root);
0579         goto out_brelse_bh2;
0580     }
0581 
0582     sb->s_root = d_make_root(root);
0583     if (!sb->s_root) {
0584         ret = -ENOMEM;
0585         goto out_brelse_bh2;
0586     }
0587     printk(KERN_DEBUG "omfs: Mounted volume %s\n", omfs_rb->r_name);
0588 
0589     ret = 0;
0590 out_brelse_bh2:
0591     brelse(bh2);
0592 out_brelse_bh:
0593     brelse(bh);
0594 end:
0595     if (ret)
0596         kfree(sbi);
0597     return ret;
0598 }
0599 
0600 static struct dentry *omfs_mount(struct file_system_type *fs_type,
0601             int flags, const char *dev_name, void *data)
0602 {
0603     return mount_bdev(fs_type, flags, dev_name, data, omfs_fill_super);
0604 }
0605 
0606 static struct file_system_type omfs_fs_type = {
0607     .owner = THIS_MODULE,
0608     .name = "omfs",
0609     .mount = omfs_mount,
0610     .kill_sb = kill_block_super,
0611     .fs_flags = FS_REQUIRES_DEV,
0612 };
0613 MODULE_ALIAS_FS("omfs");
0614 
0615 static int __init init_omfs_fs(void)
0616 {
0617     return register_filesystem(&omfs_fs_type);
0618 }
0619 
0620 static void __exit exit_omfs_fs(void)
0621 {
0622     unregister_filesystem(&omfs_fs_type);
0623 }
0624 
0625 module_init(init_omfs_fs);
0626 module_exit(exit_omfs_fs);