root/fs/minix/inode.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. minix_put_inode
  2. minix_put_super
  3. minix_read_super
  4. minix_statfs
  5. _minix_bmap
  6. minix_bmap
  7. minix_create_block
  8. minix_read_inode
  9. minix_write_inode

   1 /*
   2  *  linux/fs/minix/inode.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <linux/sched.h>
   8 #include <linux/minix_fs.h>
   9 #include <linux/kernel.h>
  10 #include <linux/mm.h>
  11 #include <linux/string.h>
  12 #include <linux/stat.h>
  13 
  14 #include <asm/system.h>
  15 #include <asm/segment.h>
  16 
  17 int sync_dev(int dev);
  18 
  19 void minix_put_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  20 {
  21         inode->i_size = 0;
  22         minix_truncate(inode);
  23         minix_free_inode(inode);
  24 }
  25 
  26 void minix_put_super(struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  27 {
  28         int i;
  29 
  30         lock_super(sb);
  31         sb->s_dev = 0;
  32         for(i = 0 ; i < MINIX_I_MAP_SLOTS ; i++)
  33                 brelse(sb->u.minix_sb.s_imap[i]);
  34         for(i = 0 ; i < MINIX_Z_MAP_SLOTS ; i++)
  35                 brelse(sb->u.minix_sb.s_zmap[i]);
  36         free_super(sb);
  37         return;
  38 }
  39 
  40 static struct super_operations minix_sops = { 
  41         minix_read_inode,
  42         minix_write_inode,
  43         minix_put_inode,
  44         minix_put_super,
  45         NULL,
  46         minix_statfs
  47 };
  48 
  49 struct super_block *minix_read_super(struct super_block *s,void *data)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51         struct buffer_head *bh;
  52         struct minix_super_block *ms;
  53         int i,dev=s->s_dev,block;
  54 
  55         lock_super(s);
  56         if (!(bh = bread(dev,1,BLOCK_SIZE))) {
  57                 s->s_dev=0;
  58                 free_super(s);
  59                 printk("bread failed\n");
  60                 return NULL;
  61         }
  62         ms = (struct minix_super_block *) bh->b_data;
  63         s->s_blocksize = 1024;
  64         s->u.minix_sb.s_ninodes = ms->s_ninodes;
  65         s->u.minix_sb.s_nzones = ms->s_nzones;
  66         s->u.minix_sb.s_imap_blocks = ms->s_imap_blocks;
  67         s->u.minix_sb.s_zmap_blocks = ms->s_zmap_blocks;
  68         s->u.minix_sb.s_firstdatazone = ms->s_firstdatazone;
  69         s->u.minix_sb.s_log_zone_size = ms->s_log_zone_size;
  70         s->u.minix_sb.s_max_size = ms->s_max_size;
  71         s->s_magic = ms->s_magic;
  72         brelse(bh);
  73         if (s->s_magic != MINIX_SUPER_MAGIC) {
  74                 s->s_dev = 0;
  75                 free_super(s);
  76                 printk("magic match failed\n");
  77                 return NULL;
  78         }
  79         for (i=0;i < MINIX_I_MAP_SLOTS;i++)
  80                 s->u.minix_sb.s_imap[i] = NULL;
  81         for (i=0;i < MINIX_Z_MAP_SLOTS;i++)
  82                 s->u.minix_sb.s_zmap[i] = NULL;
  83         block=2;
  84         for (i=0 ; i < s->u.minix_sb.s_imap_blocks ; i++)
  85                 if (s->u.minix_sb.s_imap[i]=bread(dev,block,BLOCK_SIZE))
  86                         block++;
  87                 else
  88                         break;
  89         for (i=0 ; i < s->u.minix_sb.s_zmap_blocks ; i++)
  90                 if (s->u.minix_sb.s_zmap[i]=bread(dev,block,BLOCK_SIZE))
  91                         block++;
  92                 else
  93                         break;
  94         if (block != 2+s->u.minix_sb.s_imap_blocks+s->u.minix_sb.s_zmap_blocks) {
  95                 for(i=0;i<MINIX_I_MAP_SLOTS;i++)
  96                         brelse(s->u.minix_sb.s_imap[i]);
  97                 for(i=0;i<MINIX_Z_MAP_SLOTS;i++)
  98                         brelse(s->u.minix_sb.s_zmap[i]);
  99                 s->s_dev=0;
 100                 free_super(s);
 101                 printk("block failed\n");
 102                 return NULL;
 103         }
 104         s->u.minix_sb.s_imap[0]->b_data[0] |= 1;
 105         s->u.minix_sb.s_zmap[0]->b_data[0] |= 1;
 106         free_super(s);
 107         /* set up enough so that it can read an inode */
 108         s->s_dev = dev;
 109         s->s_op = &minix_sops;
 110         if (!(s->s_mounted = iget(dev,MINIX_ROOT_INO))) {
 111                 s->s_dev=0;
 112                 printk("get root inode failed\n");
 113                 return NULL;
 114         }
 115         return s;
 116 }
 117 
 118 void minix_statfs (struct super_block *sb, struct statfs *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 119 {
 120         long tmp;
 121 
 122         put_fs_long(MINIX_SUPER_MAGIC, &buf->f_type);
 123         put_fs_long(1024, &buf->f_bsize);
 124         put_fs_long(sb->u.minix_sb.s_nzones << sb->u.minix_sb.s_log_zone_size, &buf->f_blocks);
 125         tmp = minix_count_free_blocks(sb);
 126         put_fs_long(tmp, &buf->f_bfree);
 127         put_fs_long(tmp, &buf->f_bavail);
 128         put_fs_long(sb->u.minix_sb.s_ninodes, &buf->f_files);
 129         put_fs_long(minix_count_free_inodes(sb), &buf->f_ffree);
 130         /* Don't know what value to put in buf->f_fsid */
 131 }
 132 
 133 static int _minix_bmap(struct inode * inode,int block,int create)
     /* [previous][next][first][last][top][bottom][index][help] */
 134 {
 135         struct buffer_head * bh;
 136         int i;
 137 
 138         if (block<0) {
 139                 printk("_minix_bmap: block<0");
 140                 return 0;
 141         }
 142         if (block >= 7+512+512*512) {
 143                 printk("_minix_bmap: block>big");
 144                 return 0;
 145         }
 146         if (block<7) {
 147                 if (create && !inode->i_data[block])
 148                         if (inode->i_data[block]=minix_new_block(inode->i_dev)) {
 149                                 inode->i_ctime=CURRENT_TIME;
 150                                 inode->i_dirt=1;
 151                         }
 152                 return inode->i_data[block];
 153         }
 154         block -= 7;
 155         if (block<512) {
 156                 if (create && !inode->i_data[7])
 157                         if (inode->i_data[7]=minix_new_block(inode->i_dev)) {
 158                                 inode->i_dirt=1;
 159                                 inode->i_ctime=CURRENT_TIME;
 160                         }
 161                 if (!inode->i_data[7])
 162                         return 0;
 163                 if (!(bh = bread(inode->i_dev,inode->i_data[7],BLOCK_SIZE)))
 164                         return 0;
 165                 i = ((unsigned short *) (bh->b_data))[block];
 166                 if (create && !i)
 167                         if (i=minix_new_block(inode->i_dev)) {
 168                                 ((unsigned short *) (bh->b_data))[block]=i;
 169                                 bh->b_dirt=1;
 170                         }
 171                 brelse(bh);
 172                 return i;
 173         }
 174         block -= 512;
 175         if (create && !inode->i_data[8])
 176                 if (inode->i_data[8]=minix_new_block(inode->i_dev)) {
 177                         inode->i_dirt=1;
 178                         inode->i_ctime=CURRENT_TIME;
 179                 }
 180         if (!inode->i_data[8])
 181                 return 0;
 182         if (!(bh=bread(inode->i_dev,inode->i_data[8], BLOCK_SIZE)))
 183                 return 0;
 184         i = ((unsigned short *)bh->b_data)[block>>9];
 185         if (create && !i)
 186                 if (i=minix_new_block(inode->i_dev)) {
 187                         ((unsigned short *) (bh->b_data))[block>>9]=i;
 188                         bh->b_dirt=1;
 189                 }
 190         brelse(bh);
 191         if (!i)
 192                 return 0;
 193         if (!(bh=bread(inode->i_dev,i,BLOCK_SIZE)))
 194                 return 0;
 195         i = ((unsigned short *)bh->b_data)[block&511];
 196         if (create && !i)
 197                 if (i=minix_new_block(inode->i_dev)) {
 198                         ((unsigned short *) (bh->b_data))[block&511]=i;
 199                         bh->b_dirt=1;
 200                 }
 201         brelse(bh);
 202         return i;
 203 }
 204 
 205 int minix_bmap(struct inode * inode,int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 206 {
 207         return _minix_bmap(inode,block,0);
 208 }
 209 
 210 int minix_create_block(struct inode * inode, int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 211 {
 212         return _minix_bmap(inode,block,1);
 213 }
 214 
 215 void minix_read_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 216 {
 217         struct buffer_head * bh;
 218         struct minix_inode * raw_inode;
 219         int block;
 220 
 221         block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
 222                 (inode->i_ino-1)/MINIX_INODES_PER_BLOCK;
 223         if (!(bh=bread(inode->i_dev,block, BLOCK_SIZE)))
 224                 panic("unable to read i-node block");
 225         raw_inode = ((struct minix_inode *) bh->b_data) +
 226                 (inode->i_ino-1)%MINIX_INODES_PER_BLOCK;
 227         inode->i_mode = raw_inode->i_mode;
 228         inode->i_uid = raw_inode->i_uid;
 229         inode->i_gid = raw_inode->i_gid;
 230         inode->i_nlink = raw_inode->i_nlinks;
 231         inode->i_size = raw_inode->i_size;
 232         inode->i_mtime = inode->i_atime = inode->i_ctime = raw_inode->i_time;
 233         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 234                 inode->i_rdev = raw_inode->i_zone[0];
 235         else for (block = 0; block < 9; block++)
 236                 inode->i_data[block] = raw_inode->i_zone[block];
 237         brelse(bh);
 238         inode->i_op = NULL;
 239         if (S_ISREG(inode->i_mode))
 240                 inode->i_op = &minix_file_inode_operations;
 241         else if (S_ISDIR(inode->i_mode))
 242                 inode->i_op = &minix_dir_inode_operations;
 243         else if (S_ISLNK(inode->i_mode))
 244                 inode->i_op = &minix_symlink_inode_operations;
 245         else if (S_ISCHR(inode->i_mode))
 246                 inode->i_op = &minix_chrdev_inode_operations;
 247         else if (S_ISBLK(inode->i_mode))
 248                 inode->i_op = &minix_blkdev_inode_operations;
 249         else if (S_ISFIFO(inode->i_mode)) {
 250                 inode->i_op = &minix_fifo_inode_operations;
 251                 inode->i_size = 0;
 252                 inode->i_pipe = 1;
 253                 PIPE_HEAD(*inode) = PIPE_TAIL(*inode) = 0;
 254                 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 0;
 255         }
 256 }
 257 
 258 void minix_write_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 259 {
 260         struct buffer_head * bh;
 261         struct minix_inode * raw_inode;
 262         int block;
 263 
 264         block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
 265                 (inode->i_ino-1)/MINIX_INODES_PER_BLOCK;
 266         if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE)))
 267                 panic("unable to read i-node block");
 268         raw_inode = ((struct minix_inode *)bh->b_data) +
 269                 (inode->i_ino-1)%MINIX_INODES_PER_BLOCK;
 270         raw_inode->i_mode = inode->i_mode;
 271         raw_inode->i_uid = inode->i_uid;
 272         raw_inode->i_gid = inode->i_gid;
 273         raw_inode->i_nlinks = inode->i_nlink;
 274         raw_inode->i_size = inode->i_size;
 275         raw_inode->i_time = inode->i_mtime;
 276         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 277                 raw_inode->i_zone[0] = inode->i_rdev;
 278         else for (block = 0; block < 9; block++)
 279                 raw_inode->i_zone[block] = inode->i_data[block];
 280         bh->b_dirt=1;
 281         inode->i_dirt=0;
 282         brelse(bh);
 283 }

/* [previous][next][first][last][top][bottom][index][help] */