linux/fs/qnx4/inode.c
<<
>>
Prefs
   1/*
   2 * QNX4 file system, Linux implementation.
   3 *
   4 * Version : 0.2.1
   5 *
   6 * Using parts of the xiafs filesystem.
   7 *
   8 * History :
   9 *
  10 * 01-06-1998 by Richard Frowijn : first release.
  11 * 20-06-1998 by Frank Denis : Linux 2.1.99+ support, boot signature, misc.
  12 * 30-06-1998 by Frank Denis : first step to write inodes.
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/init.h>
  17#include <linux/slab.h>
  18#include <linux/highuid.h>
  19#include <linux/pagemap.h>
  20#include <linux/buffer_head.h>
  21#include <linux/writeback.h>
  22#include <linux/statfs.h>
  23#include "qnx4.h"
  24
  25#define QNX4_VERSION  4
  26#define QNX4_BMNAME   ".bitmap"
  27
  28static const struct super_operations qnx4_sops;
  29
  30static struct inode *qnx4_alloc_inode(struct super_block *sb);
  31static void qnx4_destroy_inode(struct inode *inode);
  32static int qnx4_remount(struct super_block *sb, int *flags, char *data);
  33static int qnx4_statfs(struct dentry *, struct kstatfs *);
  34
  35static const struct super_operations qnx4_sops =
  36{
  37        .alloc_inode    = qnx4_alloc_inode,
  38        .destroy_inode  = qnx4_destroy_inode,
  39        .statfs         = qnx4_statfs,
  40        .remount_fs     = qnx4_remount,
  41};
  42
  43static int qnx4_remount(struct super_block *sb, int *flags, char *data)
  44{
  45        struct qnx4_sb_info *qs;
  46
  47        qs = qnx4_sb(sb);
  48        qs->Version = QNX4_VERSION;
  49        *flags |= MS_RDONLY;
  50        return 0;
  51}
  52
  53static int qnx4_get_block( struct inode *inode, sector_t iblock, struct buffer_head *bh, int create )
  54{
  55        unsigned long phys;
  56
  57        QNX4DEBUG((KERN_INFO "qnx4: qnx4_get_block inode=[%ld] iblock=[%ld]\n",inode->i_ino,iblock));
  58
  59        phys = qnx4_block_map( inode, iblock );
  60        if ( phys ) {
  61                // logical block is before EOF
  62                map_bh(bh, inode->i_sb, phys);
  63        }
  64        return 0;
  65}
  66
  67static inline u32 try_extent(qnx4_xtnt_t *extent, u32 *offset)
  68{
  69        u32 size = le32_to_cpu(extent->xtnt_size);
  70        if (*offset < size)
  71                return le32_to_cpu(extent->xtnt_blk) + *offset - 1;
  72        *offset -= size;
  73        return 0;
  74}
  75
  76unsigned long qnx4_block_map( struct inode *inode, long iblock )
  77{
  78        int ix;
  79        long i_xblk;
  80        struct buffer_head *bh = NULL;
  81        struct qnx4_xblk *xblk = NULL;
  82        struct qnx4_inode_entry *qnx4_inode = qnx4_raw_inode(inode);
  83        u16 nxtnt = le16_to_cpu(qnx4_inode->di_num_xtnts);
  84        u32 offset = iblock;
  85        u32 block = try_extent(&qnx4_inode->di_first_xtnt, &offset);
  86
  87        if (block) {
  88                // iblock is in the first extent. This is easy.
  89        } else {
  90                // iblock is beyond first extent. We have to follow the extent chain.
  91                i_xblk = le32_to_cpu(qnx4_inode->di_xblk);
  92                ix = 0;
  93                while ( --nxtnt > 0 ) {
  94                        if ( ix == 0 ) {
  95                                // read next xtnt block.
  96                                bh = sb_bread(inode->i_sb, i_xblk - 1);
  97                                if ( !bh ) {
  98                                        QNX4DEBUG((KERN_ERR "qnx4: I/O error reading xtnt block [%ld])\n", i_xblk - 1));
  99                                        return -EIO;
 100                                }
 101                                xblk = (struct qnx4_xblk*)bh->b_data;
 102                                if ( memcmp( xblk->xblk_signature, "IamXblk", 7 ) ) {
 103                                        QNX4DEBUG((KERN_ERR "qnx4: block at %ld is not a valid xtnt\n", qnx4_inode->i_xblk));
 104                                        return -EIO;
 105                                }
 106                        }
 107                        block = try_extent(&xblk->xblk_xtnts[ix], &offset);
 108                        if (block) {
 109                                // got it!
 110                                break;
 111                        }
 112                        if ( ++ix >= xblk->xblk_num_xtnts ) {
 113                                i_xblk = le32_to_cpu(xblk->xblk_next_xblk);
 114                                ix = 0;
 115                                brelse( bh );
 116                                bh = NULL;
 117                        }
 118                }
 119                if ( bh )
 120                        brelse( bh );
 121        }
 122
 123        QNX4DEBUG((KERN_INFO "qnx4: mapping block %ld of inode %ld = %ld\n",iblock,inode->i_ino,block));
 124        return block;
 125}
 126
 127static int qnx4_statfs(struct dentry *dentry, struct kstatfs *buf)
 128{
 129        struct super_block *sb = dentry->d_sb;
 130        u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
 131
 132        buf->f_type    = sb->s_magic;
 133        buf->f_bsize   = sb->s_blocksize;
 134        buf->f_blocks  = le32_to_cpu(qnx4_sb(sb)->BitMap->di_size) * 8;
 135        buf->f_bfree   = qnx4_count_free_blocks(sb);
 136        buf->f_bavail  = buf->f_bfree;
 137        buf->f_namelen = QNX4_NAME_MAX;
 138        buf->f_fsid.val[0] = (u32)id;
 139        buf->f_fsid.val[1] = (u32)(id >> 32);
 140
 141        return 0;
 142}
 143
 144/*
 145 * Check the root directory of the filesystem to make sure
 146 * it really _is_ a qnx4 filesystem, and to check the size
 147 * of the directory entry.
 148 */
 149static const char *qnx4_checkroot(struct super_block *sb,
 150                                  struct qnx4_super_block *s)
 151{
 152        struct buffer_head *bh;
 153        struct qnx4_inode_entry *rootdir;
 154        int rd, rl;
 155        int i, j;
 156
 157        if (s->RootDir.di_fname[0] != '/' || s->RootDir.di_fname[1] != '\0')
 158                return "no qnx4 filesystem (no root dir).";
 159        QNX4DEBUG((KERN_NOTICE "QNX4 filesystem found on dev %s.\n", sb->s_id));
 160        rd = le32_to_cpu(s->RootDir.di_first_xtnt.xtnt_blk) - 1;
 161        rl = le32_to_cpu(s->RootDir.di_first_xtnt.xtnt_size);
 162        for (j = 0; j < rl; j++) {
 163                bh = sb_bread(sb, rd + j);      /* root dir, first block */
 164                if (bh == NULL)
 165                        return "unable to read root entry.";
 166                rootdir = (struct qnx4_inode_entry *) bh->b_data;
 167                for (i = 0; i < QNX4_INODES_PER_BLOCK; i++, rootdir++) {
 168                        QNX4DEBUG((KERN_INFO "rootdir entry found : [%s]\n", rootdir->di_fname));
 169                        if (strcmp(rootdir->di_fname, QNX4_BMNAME) != 0)
 170                                continue;
 171                        qnx4_sb(sb)->BitMap = kmemdup(rootdir,
 172                                                      sizeof(struct qnx4_inode_entry),
 173                                                      GFP_KERNEL);
 174                        brelse(bh);
 175                        if (!qnx4_sb(sb)->BitMap)
 176                                return "not enough memory for bitmap inode";
 177                        /* keep bitmap inode known */
 178                        return NULL;
 179                }
 180                brelse(bh);
 181        }
 182        return "bitmap file not found.";
 183}
 184
 185static int qnx4_fill_super(struct super_block *s, void *data, int silent)
 186{
 187        struct buffer_head *bh;
 188        struct inode *root;
 189        const char *errmsg;
 190        struct qnx4_sb_info *qs;
 191
 192        qs = kzalloc(sizeof(struct qnx4_sb_info), GFP_KERNEL);
 193        if (!qs)
 194                return -ENOMEM;
 195        s->s_fs_info = qs;
 196
 197        sb_set_blocksize(s, QNX4_BLOCK_SIZE);
 198
 199        s->s_op = &qnx4_sops;
 200        s->s_magic = QNX4_SUPER_MAGIC;
 201        s->s_flags |= MS_RDONLY;        /* Yup, read-only yet */
 202
 203        /* Check the superblock signature. Since the qnx4 code is
 204           dangerous, we should leave as quickly as possible
 205           if we don't belong here... */
 206        bh = sb_bread(s, 1);
 207        if (!bh) {
 208                printk(KERN_ERR "qnx4: unable to read the superblock\n");
 209                return -EINVAL;
 210        }
 211
 212        /* check before allocating dentries, inodes, .. */
 213        errmsg = qnx4_checkroot(s, (struct qnx4_super_block *) bh->b_data);
 214        brelse(bh);
 215        if (errmsg != NULL) {
 216                if (!silent)
 217                        printk(KERN_ERR "qnx4: %s\n", errmsg);
 218                return -EINVAL;
 219        }
 220
 221        /* does root not have inode number QNX4_ROOT_INO ?? */
 222        root = qnx4_iget(s, QNX4_ROOT_INO * QNX4_INODES_PER_BLOCK);
 223        if (IS_ERR(root)) {
 224                printk(KERN_ERR "qnx4: get inode failed\n");
 225                return PTR_ERR(root);
 226        }
 227
 228        s->s_root = d_make_root(root);
 229        if (s->s_root == NULL)
 230                return -ENOMEM;
 231
 232        return 0;
 233}
 234
 235static void qnx4_kill_sb(struct super_block *sb)
 236{
 237        struct qnx4_sb_info *qs = qnx4_sb(sb);
 238        kill_block_super(sb);
 239        if (qs) {
 240                kfree(qs->BitMap);
 241                kfree(qs);
 242        }
 243}
 244
 245static int qnx4_readpage(struct file *file, struct page *page)
 246{
 247        return block_read_full_page(page,qnx4_get_block);
 248}
 249
 250static sector_t qnx4_bmap(struct address_space *mapping, sector_t block)
 251{
 252        return generic_block_bmap(mapping,block,qnx4_get_block);
 253}
 254static const struct address_space_operations qnx4_aops = {
 255        .readpage       = qnx4_readpage,
 256        .bmap           = qnx4_bmap
 257};
 258
 259struct inode *qnx4_iget(struct super_block *sb, unsigned long ino)
 260{
 261        struct buffer_head *bh;
 262        struct qnx4_inode_entry *raw_inode;
 263        int block;
 264        struct qnx4_inode_entry *qnx4_inode;
 265        struct inode *inode;
 266
 267        inode = iget_locked(sb, ino);
 268        if (!inode)
 269                return ERR_PTR(-ENOMEM);
 270        if (!(inode->i_state & I_NEW))
 271                return inode;
 272
 273        qnx4_inode = qnx4_raw_inode(inode);
 274        inode->i_mode = 0;
 275
 276        QNX4DEBUG((KERN_INFO "reading inode : [%d]\n", ino));
 277        if (!ino) {
 278                printk(KERN_ERR "qnx4: bad inode number on dev %s: %lu is "
 279                                "out of range\n",
 280                       sb->s_id, ino);
 281                iget_failed(inode);
 282                return ERR_PTR(-EIO);
 283        }
 284        block = ino / QNX4_INODES_PER_BLOCK;
 285
 286        if (!(bh = sb_bread(sb, block))) {
 287                printk(KERN_ERR "qnx4: major problem: unable to read inode from dev "
 288                       "%s\n", sb->s_id);
 289                iget_failed(inode);
 290                return ERR_PTR(-EIO);
 291        }
 292        raw_inode = ((struct qnx4_inode_entry *) bh->b_data) +
 293            (ino % QNX4_INODES_PER_BLOCK);
 294
 295        inode->i_mode    = le16_to_cpu(raw_inode->di_mode);
 296        i_uid_write(inode, (uid_t)le16_to_cpu(raw_inode->di_uid));
 297        i_gid_write(inode, (gid_t)le16_to_cpu(raw_inode->di_gid));
 298        set_nlink(inode, le16_to_cpu(raw_inode->di_nlink));
 299        inode->i_size    = le32_to_cpu(raw_inode->di_size);
 300        inode->i_mtime.tv_sec   = le32_to_cpu(raw_inode->di_mtime);
 301        inode->i_mtime.tv_nsec = 0;
 302        inode->i_atime.tv_sec   = le32_to_cpu(raw_inode->di_atime);
 303        inode->i_atime.tv_nsec = 0;
 304        inode->i_ctime.tv_sec   = le32_to_cpu(raw_inode->di_ctime);
 305        inode->i_ctime.tv_nsec = 0;
 306        inode->i_blocks  = le32_to_cpu(raw_inode->di_first_xtnt.xtnt_size);
 307
 308        memcpy(qnx4_inode, raw_inode, QNX4_DIR_ENTRY_SIZE);
 309        if (S_ISREG(inode->i_mode)) {
 310                inode->i_fop = &generic_ro_fops;
 311                inode->i_mapping->a_ops = &qnx4_aops;
 312                qnx4_i(inode)->mmu_private = inode->i_size;
 313        } else if (S_ISDIR(inode->i_mode)) {
 314                inode->i_op = &qnx4_dir_inode_operations;
 315                inode->i_fop = &qnx4_dir_operations;
 316        } else if (S_ISLNK(inode->i_mode)) {
 317                inode->i_op = &page_symlink_inode_operations;
 318                inode->i_mapping->a_ops = &qnx4_aops;
 319                qnx4_i(inode)->mmu_private = inode->i_size;
 320        } else {
 321                printk(KERN_ERR "qnx4: bad inode %lu on dev %s\n",
 322                        ino, sb->s_id);
 323                iget_failed(inode);
 324                brelse(bh);
 325                return ERR_PTR(-EIO);
 326        }
 327        brelse(bh);
 328        unlock_new_inode(inode);
 329        return inode;
 330}
 331
 332static struct kmem_cache *qnx4_inode_cachep;
 333
 334static struct inode *qnx4_alloc_inode(struct super_block *sb)
 335{
 336        struct qnx4_inode_info *ei;
 337        ei = kmem_cache_alloc(qnx4_inode_cachep, GFP_KERNEL);
 338        if (!ei)
 339                return NULL;
 340        return &ei->vfs_inode;
 341}
 342
 343static void qnx4_i_callback(struct rcu_head *head)
 344{
 345        struct inode *inode = container_of(head, struct inode, i_rcu);
 346        kmem_cache_free(qnx4_inode_cachep, qnx4_i(inode));
 347}
 348
 349static void qnx4_destroy_inode(struct inode *inode)
 350{
 351        call_rcu(&inode->i_rcu, qnx4_i_callback);
 352}
 353
 354static void init_once(void *foo)
 355{
 356        struct qnx4_inode_info *ei = (struct qnx4_inode_info *) foo;
 357
 358        inode_init_once(&ei->vfs_inode);
 359}
 360
 361static int init_inodecache(void)
 362{
 363        qnx4_inode_cachep = kmem_cache_create("qnx4_inode_cache",
 364                                             sizeof(struct qnx4_inode_info),
 365                                             0, (SLAB_RECLAIM_ACCOUNT|
 366                                                SLAB_MEM_SPREAD),
 367                                             init_once);
 368        if (qnx4_inode_cachep == NULL)
 369                return -ENOMEM;
 370        return 0;
 371}
 372
 373static void destroy_inodecache(void)
 374{
 375        /*
 376         * Make sure all delayed rcu free inodes are flushed before we
 377         * destroy cache.
 378         */
 379        rcu_barrier();
 380        kmem_cache_destroy(qnx4_inode_cachep);
 381}
 382
 383static struct dentry *qnx4_mount(struct file_system_type *fs_type,
 384        int flags, const char *dev_name, void *data)
 385{
 386        return mount_bdev(fs_type, flags, dev_name, data, qnx4_fill_super);
 387}
 388
 389static struct file_system_type qnx4_fs_type = {
 390        .owner          = THIS_MODULE,
 391        .name           = "qnx4",
 392        .mount          = qnx4_mount,
 393        .kill_sb        = qnx4_kill_sb,
 394        .fs_flags       = FS_REQUIRES_DEV,
 395};
 396MODULE_ALIAS_FS("qnx4");
 397
 398static int __init init_qnx4_fs(void)
 399{
 400        int err;
 401
 402        err = init_inodecache();
 403        if (err)
 404                return err;
 405
 406        err = register_filesystem(&qnx4_fs_type);
 407        if (err) {
 408                destroy_inodecache();
 409                return err;
 410        }
 411
 412        printk(KERN_INFO "QNX4 filesystem 0.2.3 registered.\n");
 413        return 0;
 414}
 415
 416static void __exit exit_qnx4_fs(void)
 417{
 418        unregister_filesystem(&qnx4_fs_type);
 419        destroy_inodecache();
 420}
 421
 422module_init(init_qnx4_fs)
 423module_exit(exit_qnx4_fs)
 424MODULE_LICENSE("GPL");
 425
 426