Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * QNX4 file system, Linux implementation.
0004  *
0005  * Version : 0.2.1
0006  *
0007  * Using parts of the xiafs filesystem.
0008  *
0009  * History :
0010  *
0011  * 01-06-1998 by Richard Frowijn : first release.
0012  * 20-06-1998 by Frank Denis : Linux 2.1.99+ support, boot signature, misc.
0013  * 30-06-1998 by Frank Denis : first step to write inodes.
0014  */
0015 
0016 #include <linux/module.h>
0017 #include <linux/init.h>
0018 #include <linux/slab.h>
0019 #include <linux/highuid.h>
0020 #include <linux/pagemap.h>
0021 #include <linux/buffer_head.h>
0022 #include <linux/writeback.h>
0023 #include <linux/statfs.h>
0024 #include "qnx4.h"
0025 
0026 #define QNX4_VERSION  4
0027 #define QNX4_BMNAME   ".bitmap"
0028 
0029 static const struct super_operations qnx4_sops;
0030 
0031 static struct inode *qnx4_alloc_inode(struct super_block *sb);
0032 static void qnx4_free_inode(struct inode *inode);
0033 static int qnx4_remount(struct super_block *sb, int *flags, char *data);
0034 static int qnx4_statfs(struct dentry *, struct kstatfs *);
0035 
0036 static const struct super_operations qnx4_sops =
0037 {
0038     .alloc_inode    = qnx4_alloc_inode,
0039     .free_inode = qnx4_free_inode,
0040     .statfs     = qnx4_statfs,
0041     .remount_fs = qnx4_remount,
0042 };
0043 
0044 static int qnx4_remount(struct super_block *sb, int *flags, char *data)
0045 {
0046     struct qnx4_sb_info *qs;
0047 
0048     sync_filesystem(sb);
0049     qs = qnx4_sb(sb);
0050     qs->Version = QNX4_VERSION;
0051     *flags |= SB_RDONLY;
0052     return 0;
0053 }
0054 
0055 static int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
0056 {
0057     unsigned long phys;
0058 
0059     QNX4DEBUG((KERN_INFO "qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
0060 
0061     phys = qnx4_block_map( inode, iblock );
0062     if ( phys ) {
0063         // logical block is before EOF
0064         map_bh(bh, inode->i_sb, phys);
0065     }
0066     return 0;
0067 }
0068 
0069 static inline u32 try_extent(qnx4_xtnt_t *extent, u32 *offset)
0070 {
0071     u32 size = le32_to_cpu(extent->xtnt_size);
0072     if (*offset < size)
0073         return le32_to_cpu(extent->xtnt_blk) + *offset - 1;
0074     *offset -= size;
0075     return 0;
0076 }
0077 
0078 unsigned long qnx4_block_map( struct inode *inode, long iblock )
0079 {
0080     int ix;
0081     long i_xblk;
0082     struct buffer_head *bh = NULL;
0083     struct qnx4_xblk *xblk = NULL;
0084     struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
0085     u16 nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts);
0086     u32 offset = iblock;
0087     u32 block = try_extent(&qnx4_inode->di_first_xtnt, &offset);
0088 
0089     if (block) {
0090         // iblock is in the first extent. This is easy.
0091     } else {
0092         // iblock is beyond first extent. We have to follow the extent chain.
0093         i_xblk = le32_to_cpu(qnx4_inode->di_xblk);
0094         ix = 0;
0095         while ( --nxtnt > 0 ) {
0096             if ( ix == 0 ) {
0097                 // read next xtnt block.
0098                 bh = sb_bread(inode->i_sb, i_xblk - 1);
0099                 if ( !bh ) {
0100                     QNX4DEBUG((KERN_ERR "qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
0101                     return -EIO;
0102                 }
0103                 xblk = (struct qnx4_xblk*)bh->b_data;
0104                 if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) {
0105                     QNX4DEBUG((KERN_ERR "qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk));
0106                     return -EIO;
0107                 }
0108             }
0109             block = try_extent(&xblk->xblk_xtnts[ix], &offset);
0110             if (block) {
0111                 // got it!
0112                 break;
0113             }
0114             if ( ++ix >= xblk->xblk_num_xtnts ) {
0115                 i_xblk = le32_to_cpu(xblk->xblk_next_xblk);
0116                 ix = 0;
0117                 brelse( bh );
0118                 bh = NULL;
0119             }
0120         }
0121         if ( bh )
0122             brelse( bh );
0123     }
0124 
0125     QNX4DEBUG((KERN_INFO "qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
0126     return block;
0127 }
0128 
0129 static int qnx4_statfs(struct dentry *dentry, struct kstatfs *buf)
0130 {
0131     struct super_block *sb = dentry->d_sb;
0132     u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
0133 
0134     buf->f_type    = sb->s_magic;
0135     buf->f_bsize   = sb->s_blocksize;
0136     buf->f_blocks  = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8;
0137     buf->f_bfree   = qnx4_count_free_blocks(sb);
0138     buf->f_bavail  = buf->f_bfree;
0139     buf->f_namelen = QNX4_NAME_MAX;
0140     buf->f_fsid    = u64_to_fsid(id);
0141 
0142     return 0;
0143 }
0144 
0145 /*
0146  * Check the root directory of the filesystem to make sure
0147  * it really _is_ a qnx4 filesystem, and to check the size
0148  * of the directory entry.
0149  */
0150 static const char *qnx4_checkroot(struct super_block *sb,
0151                   struct qnx4_super_block *s)
0152 {
0153     struct buffer_head *bh;
0154     struct qnx4_inode_entry *rootdir;
0155     int rd, rl;
0156     int i, j;
0157 
0158     if (s->RootDir.di_fname[0] != '/' || s->RootDir.di_fname[1] != '\0')
0159         return "no qnx4 filesystem (no root dir).";
0160     QNX4DEBUG((KERN_NOTICE "QNX4 filesystem found on dev %s.\n", sb->s_id));
0161     rd = le32_to_cpu(s->RootDir.di_first_xtnt.xtnt_blk) - 1;
0162     rl = le32_to_cpu(s->RootDir.di_first_xtnt.xtnt_size);
0163     for (j = 0; j < rl; j++) {
0164         bh = sb_bread(sb, rd + j);  /* root dir, first block */
0165         if (bh == NULL)
0166             return "unable to read root entry.";
0167         rootdir = (struct qnx4_inode_entry *) bh->b_data;
0168         for (i = 0; i < QNX4_INODES_PER_BLOCK; i++, rootdir++) {
0169             QNX4DEBUG((KERN_INFO "rootdir entry found : [%s]\n", rootdir->di_fname));
0170             if (strcmp(rootdir->di_fname, QNX4_BMNAME) != 0)
0171                 continue;
0172             qnx4_sb(sb)->BitMap = kmemdup(rootdir,
0173                               sizeof(struct qnx4_inode_entry),
0174                               GFP_KERNEL);
0175             brelse(bh);
0176             if (!qnx4_sb(sb)->BitMap)
0177                 return "not enough memory for bitmap inode";
0178             /* keep bitmap inode known */
0179             return NULL;
0180         }
0181         brelse(bh);
0182     }
0183     return "bitmap file not found.";
0184 }
0185 
0186 static int qnx4_fill_super(struct super_block *s, void *data, int silent)
0187 {
0188     struct buffer_head *bh;
0189     struct inode *root;
0190     const char *errmsg;
0191     struct qnx4_sb_info *qs;
0192 
0193     qs = kzalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
0194     if (!qs)
0195         return -ENOMEM;
0196     s->s_fs_info = qs;
0197 
0198     sb_set_blocksize(s, QNX4_BLOCK_SIZE);
0199 
0200     s->s_op = &qnx4_sops;
0201     s->s_magic = QNX4_SUPER_MAGIC;
0202     s->s_flags |= SB_RDONLY;    /* Yup, read-only yet */
0203     s->s_time_min = 0;
0204     s->s_time_max = U32_MAX;
0205 
0206     /* Check the superblock signature. Since the qnx4 code is
0207        dangerous, we should leave as quickly as possible
0208        if we don't belong here... */
0209     bh = sb_bread(s, 1);
0210     if (!bh) {
0211         printk(KERN_ERR "qnx4: unable to read the superblock\n");
0212         return -EINVAL;
0213     }
0214 
0215     /* check before allocating dentries, inodes, .. */
0216     errmsg = qnx4_checkroot(s, (struct qnx4_super_block *) bh->b_data);
0217     brelse(bh);
0218     if (errmsg != NULL) {
0219         if (!silent)
0220             printk(KERN_ERR "qnx4: %s\n", errmsg);
0221         return -EINVAL;
0222     }
0223 
0224     /* does root not have inode number QNX4_ROOT_INO ?? */
0225     root = qnx4_iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
0226     if (IS_ERR(root)) {
0227         printk(KERN_ERR "qnx4: get inode failed\n");
0228         return PTR_ERR(root);
0229     }
0230 
0231     s->s_root = d_make_root(root);
0232     if (s->s_root == NULL)
0233         return -ENOMEM;
0234 
0235     return 0;
0236 }
0237 
0238 static void qnx4_kill_sb(struct super_block *sb)
0239 {
0240     struct qnx4_sb_info *qs = qnx4_sb(sb);
0241     kill_block_super(sb);
0242     if (qs) {
0243         kfree(qs->BitMap);
0244         kfree(qs);
0245     }
0246 }
0247 
0248 static int qnx4_read_folio(struct file *file, struct folio *folio)
0249 {
0250     return block_read_full_folio(folio, qnx4_get_block);
0251 }
0252 
0253 static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
0254 {
0255     return generic_block_bmap(mapping,block,qnx4_get_block);
0256 }
0257 
0258 static const struct address_space_operations qnx4_aops = {
0259     .read_folio = qnx4_read_folio,
0260     .bmap       = qnx4_bmap
0261 };
0262 
0263 struct inode *qnx4_iget(struct super_block *sb, unsigned long ino)
0264 {
0265     struct buffer_head *bh;
0266     struct qnx4_inode_entry *raw_inode;
0267     int block;
0268     struct qnx4_inode_entry *qnx4_inode;
0269     struct inode *inode;
0270 
0271     inode = iget_locked(sb, ino);
0272     if (!inode)
0273         return ERR_PTR(-ENOMEM);
0274     if (!(inode->i_state & I_NEW))
0275         return inode;
0276 
0277     qnx4_inode = qnx4_raw_inode(inode);
0278     inode->i_mode = 0;
0279 
0280     QNX4DEBUG((KERN_INFO "reading inode : [%d]\n", ino));
0281     if (!ino) {
0282         printk(KERN_ERR "qnx4: bad inode number on dev %s: %lu is "
0283                 "out of range\n",
0284                sb->s_id, ino);
0285         iget_failed(inode);
0286         return ERR_PTR(-EIO);
0287     }
0288     block = ino / QNX4_INODES_PER_BLOCK;
0289 
0290     if (!(bh = sb_bread(sb, block))) {
0291         printk(KERN_ERR "qnx4: major problem: unable to read inode from dev "
0292                "%s\n", sb->s_id);
0293         iget_failed(inode);
0294         return ERR_PTR(-EIO);
0295     }
0296     raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
0297         (ino % QNX4_INODES_PER_BLOCK);
0298 
0299     inode->i_mode    = le16_to_cpu(raw_inode->di_mode);
0300     i_uid_write(inode, (uid_t)le16_to_cpu(raw_inode->di_uid));
0301     i_gid_write(inode, (gid_t)le16_to_cpu(raw_inode->di_gid));
0302     set_nlink(inode, le16_to_cpu(raw_inode->di_nlink));
0303     inode->i_size    = le32_to_cpu(raw_inode->di_size);
0304     inode->i_mtime.tv_sec   = le32_to_cpu(raw_inode->di_mtime);
0305     inode->i_mtime.tv_nsec = 0;
0306     inode->i_atime.tv_sec   = le32_to_cpu(raw_inode->di_atime);
0307     inode->i_atime.tv_nsec = 0;
0308     inode->i_ctime.tv_sec   = le32_to_cpu(raw_inode->di_ctime);
0309     inode->i_ctime.tv_nsec = 0;
0310     inode->i_blocks  = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size);
0311 
0312     memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE);
0313     if (S_ISREG(inode->i_mode)) {
0314         inode->i_fop = &generic_ro_fops;
0315         inode->i_mapping->a_ops = &qnx4_aops;
0316         qnx4_i(inode)->mmu_private = inode->i_size;
0317     } else if (S_ISDIR(inode->i_mode)) {
0318         inode->i_op = &qnx4_dir_inode_operations;
0319         inode->i_fop = &qnx4_dir_operations;
0320     } else if (S_ISLNK(inode->i_mode)) {
0321         inode->i_op = &page_symlink_inode_operations;
0322         inode_nohighmem(inode);
0323         inode->i_mapping->a_ops = &qnx4_aops;
0324         qnx4_i(inode)->mmu_private = inode->i_size;
0325     } else {
0326         printk(KERN_ERR "qnx4: bad inode %lu on dev %s\n",
0327             ino, sb->s_id);
0328         iget_failed(inode);
0329         brelse(bh);
0330         return ERR_PTR(-EIO);
0331     }
0332     brelse(bh);
0333     unlock_new_inode(inode);
0334     return inode;
0335 }
0336 
0337 static struct kmem_cache *qnx4_inode_cachep;
0338 
0339 static struct inode *qnx4_alloc_inode(struct super_block *sb)
0340 {
0341     struct qnx4_inode_info *ei;
0342     ei = alloc_inode_sb(sb, qnx4_inode_cachep, GFP_KERNEL);
0343     if (!ei)
0344         return NULL;
0345     return &ei->vfs_inode;
0346 }
0347 
0348 static void qnx4_free_inode(struct inode *inode)
0349 {
0350     kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
0351 }
0352 
0353 static void init_once(void *foo)
0354 {
0355     struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
0356 
0357     inode_init_once(&ei->vfs_inode);
0358 }
0359 
0360 static int init_inodecache(void)
0361 {
0362     qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
0363                          sizeof(struct qnx4_inode_info),
0364                          0, (SLAB_RECLAIM_ACCOUNT|
0365                         SLAB_MEM_SPREAD|SLAB_ACCOUNT),
0366                          init_once);
0367     if (qnx4_inode_cachep == NULL)
0368         return -ENOMEM;
0369     return 0;
0370 }
0371 
0372 static void destroy_inodecache(void)
0373 {
0374     /*
0375      * Make sure all delayed rcu free inodes are flushed before we
0376      * destroy cache.
0377      */
0378     rcu_barrier();
0379     kmem_cache_destroy(qnx4_inode_cachep);
0380 }
0381 
0382 static struct dentry *qnx4_mount(struct file_system_type *fs_type,
0383     int flags, const char *dev_name, void *data)
0384 {
0385     return mount_bdev(fs_type, flags, dev_name, data, qnx4_fill_super);
0386 }
0387 
0388 static struct file_system_type qnx4_fs_type = {
0389     .owner      = THIS_MODULE,
0390     .name       = "qnx4",
0391     .mount      = qnx4_mount,
0392     .kill_sb    = qnx4_kill_sb,
0393     .fs_flags   = FS_REQUIRES_DEV,
0394 };
0395 MODULE_ALIAS_FS("qnx4");
0396 
0397 static int __init init_qnx4_fs(void)
0398 {
0399     int err;
0400 
0401     err = init_inodecache();
0402     if (err)
0403         return err;
0404 
0405     err = register_filesystem(&qnx4_fs_type);
0406     if (err) {
0407         destroy_inodecache();
0408         return err;
0409     }
0410 
0411     printk(KERN_INFO "QNX4 filesystem 0.2.3 registered.\n");
0412     return 0;
0413 }
0414 
0415 static void __exit exit_qnx4_fs(void)
0416 {
0417     unregister_filesystem(&qnx4_fs_type);
0418     destroy_inodecache();
0419 }
0420 
0421 module_init(init_qnx4_fs)
0422 module_exit(exit_qnx4_fs)
0423 MODULE_LICENSE("GPL");
0424