root/fs/ext/inode.c

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

DEFINITIONS

This source file includes following definitions.
  1. ext_put_inode
  2. ext_put_super
  3. ext_read_super
  4. ext_write_super
  5. ext_statfs
  6. block_bmap
  7. ext_bmap
  8. inode_getblk
  9. block_getblk
  10. ext_getblk
  11. ext_bread
  12. ext_read_inode
  13. ext_write_inode

   1 /*
   2  *  linux/fs/ext/inode.c
   3  *
   4  *  Copyright (C) 1992  Remy Card (card@masi.ibp.fr)
   5  *
   6  *  from
   7  *
   8  *  linux/fs/minix/inode.c
   9  *
  10  *  Copyright (C) 1991, 1992  Linus Torvalds
  11  */
  12 
  13 #include <linux/sched.h>
  14 #include <linux/ext_fs.h>
  15 #include <linux/kernel.h>
  16 #include <linux/mm.h>
  17 #include <linux/string.h>
  18 #include <linux/stat.h>
  19 #include <linux/locks.h>
  20 
  21 #include <asm/system.h>
  22 #include <asm/segment.h>
  23 
  24 void ext_put_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26         if (inode->i_nlink)
  27                 return;
  28         inode->i_size = 0;
  29         ext_truncate(inode);
  30         ext_free_inode(inode);
  31 }
  32 
  33 void ext_put_super(struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35 
  36         lock_super(sb);
  37         sb->s_dev = 0;
  38         if (sb->u.ext_sb.s_firstfreeinodeblock)
  39                 brelse (sb->u.ext_sb.s_firstfreeinodeblock);
  40         if (sb->u.ext_sb.s_firstfreeblock)
  41                 brelse (sb->u.ext_sb.s_firstfreeblock);
  42         unlock_super(sb);
  43         return;
  44 }
  45 
  46 static struct super_operations ext_sops = { 
  47         ext_read_inode,
  48         NULL,
  49         ext_write_inode,
  50         ext_put_inode,
  51         ext_put_super,
  52         ext_write_super,
  53         ext_statfs
  54 };
  55 
  56 struct super_block *ext_read_super(struct super_block *s,void *data, 
     /* [previous][next][first][last][top][bottom][index][help] */
  57                                    int silent)
  58 {
  59         struct buffer_head *bh;
  60         struct ext_super_block *es;
  61         int dev = s->s_dev,block;
  62 
  63         lock_super(s);
  64         if (!(bh = bread(dev, 1, BLOCK_SIZE))) {
  65                 s->s_dev=0;
  66                 unlock_super(s);
  67                 printk("EXT-fs: unable to read superblock\n");
  68                 return NULL;
  69         }
  70         es = (struct ext_super_block *) bh->b_data;
  71         s->s_blocksize = 1024;
  72         s->u.ext_sb.s_ninodes = es->s_ninodes;
  73         s->u.ext_sb.s_nzones = es->s_nzones;
  74         s->u.ext_sb.s_firstdatazone = es->s_firstdatazone;
  75         s->u.ext_sb.s_log_zone_size = es->s_log_zone_size;
  76         s->u.ext_sb.s_max_size = es->s_max_size;
  77         s->s_magic = es->s_magic;
  78         s->u.ext_sb.s_firstfreeblocknumber = es->s_firstfreeblock;
  79         s->u.ext_sb.s_freeblockscount = es->s_freeblockscount;
  80         s->u.ext_sb.s_firstfreeinodenumber = es->s_firstfreeinode;
  81         s->u.ext_sb.s_freeinodescount = es->s_freeinodescount;
  82         brelse(bh);
  83         if (s->s_magic != EXT_SUPER_MAGIC) {
  84                 s->s_dev = 0;
  85                 unlock_super(s);
  86                 if (!silent)
  87                         printk("VFS: Can't find an extfs filesystem on dev 0x%04x.\n",
  88                                    dev);
  89                 return NULL;
  90         }
  91         if (!s->u.ext_sb.s_firstfreeblocknumber)
  92                 s->u.ext_sb.s_firstfreeblock = NULL;
  93         else
  94                 if (!(s->u.ext_sb.s_firstfreeblock = bread(dev,
  95                         s->u.ext_sb.s_firstfreeblocknumber, BLOCK_SIZE))) {
  96                         printk("ext_read_super: unable to read first free block\n");
  97                         s->s_dev = 0;
  98                         unlock_super(s);
  99                         return NULL;
 100                 }
 101         if (!s->u.ext_sb.s_firstfreeinodenumber)
 102                 s->u.ext_sb.s_firstfreeinodeblock = NULL;
 103         else {
 104                 block = 2 + (s->u.ext_sb.s_firstfreeinodenumber - 1) / EXT_INODES_PER_BLOCK;
 105                 if (!(s->u.ext_sb.s_firstfreeinodeblock = bread(dev, block, BLOCK_SIZE))) {
 106                         printk("ext_read_super: unable to read first free inode block\n");
 107                         brelse(s->u.ext_sb.s_firstfreeblock);
 108                         s->s_dev = 0;
 109                         unlock_super (s);
 110                         return NULL;
 111                 }
 112         }
 113         unlock_super(s);
 114         /* set up enough so that it can read an inode */
 115         s->s_dev = dev;
 116         s->s_op = &ext_sops;
 117         if (!(s->s_mounted = iget(s,EXT_ROOT_INO))) {
 118                 s->s_dev=0;
 119                 printk("EXT-fs: get root inode failed\n");
 120                 return NULL;
 121         }
 122         return s;
 123 }
 124 
 125 void ext_write_super (struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 126 {
 127         struct buffer_head * bh;
 128         struct ext_super_block * es;
 129 
 130         if (!(bh = bread(sb->s_dev, 1, BLOCK_SIZE))) {
 131                 printk ("ext_write_super: bread failed\n");
 132                 return;
 133         }
 134         es = (struct ext_super_block *) bh->b_data;
 135         es->s_firstfreeblock = sb->u.ext_sb.s_firstfreeblocknumber;
 136         es->s_freeblockscount = sb->u.ext_sb.s_freeblockscount;
 137         es->s_firstfreeinode = sb->u.ext_sb.s_firstfreeinodenumber;
 138         es->s_freeinodescount = sb->u.ext_sb.s_freeinodescount;
 139         bh->b_dirt = 1;
 140         brelse (bh);
 141         sb->s_dirt = 0;
 142 }
 143 
 144 void ext_statfs (struct super_block *sb, struct statfs *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 145 {
 146         long tmp;
 147 
 148         put_fs_long(EXT_SUPER_MAGIC, &buf->f_type);
 149         put_fs_long(1024, &buf->f_bsize);
 150         put_fs_long(sb->u.ext_sb.s_nzones << sb->u.ext_sb.s_log_zone_size,
 151                 &buf->f_blocks);
 152         tmp = ext_count_free_blocks(sb);
 153         put_fs_long(tmp, &buf->f_bfree);
 154         put_fs_long(tmp, &buf->f_bavail);
 155         put_fs_long(sb->u.ext_sb.s_ninodes, &buf->f_files);
 156         put_fs_long(ext_count_free_inodes(sb), &buf->f_ffree);
 157         put_fs_long(EXT_NAME_LEN, &buf->f_namelen);
 158         /* Don't know what value to put in buf->f_fsid */
 159 }
 160 
 161 #define inode_bmap(inode,nr) ((inode)->u.ext_i.i_data[(nr)])
 162 
 163 static int block_bmap(struct buffer_head * bh, int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 164 {
 165         int tmp;
 166 
 167         if (!bh)
 168                 return 0;
 169         tmp = ((unsigned long *) bh->b_data)[nr];
 170         brelse(bh);
 171         return tmp;
 172 }
 173 
 174 int ext_bmap(struct inode * inode,int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 175 {
 176         int i;
 177 
 178         if (block<0) {
 179                 printk("ext_bmap: block<0");
 180                 return 0;
 181         }
 182         if (block >= 9+256+256*256+256*256*256) {
 183                 printk("ext_bmap: block>big");
 184                 return 0;
 185         }
 186         if (block<9)
 187                 return inode_bmap(inode,block);
 188         block -= 9;
 189         if (block<256) {
 190                 i = inode_bmap(inode,9);
 191                 if (!i)
 192                         return 0;
 193                 return block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block);
 194         }
 195         block -= 256;
 196         if (block<256*256) {
 197                 i = inode_bmap(inode,10);
 198                 if (!i)
 199                         return 0;
 200                 i = block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block>>8);
 201                 if (!i)
 202                         return 0;
 203                 return block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block & 255);
 204         }
 205         block -= 256*256;
 206         i = inode_bmap(inode,11);
 207         if (!i)
 208                 return 0;
 209         i = block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block>>16);
 210         if (!i)
 211                 return 0;
 212         i = block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),(block>>8) & 255);
 213         if (!i)
 214                 return 0;
 215         return block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block & 255);
 216 }
 217 
 218 static struct buffer_head * inode_getblk(struct inode * inode, int nr, int create)
     /* [previous][next][first][last][top][bottom][index][help] */
 219 {
 220         int tmp;
 221         unsigned long * p;
 222         struct buffer_head * result;
 223 
 224         p = inode->u.ext_i.i_data + nr;
 225 repeat:
 226         tmp = *p;
 227         if (tmp) {
 228                 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
 229                 if (tmp == *p)
 230                         return result;
 231                 brelse(result);
 232                 goto repeat;
 233         }
 234         if (!create)
 235                 return NULL;
 236         tmp = ext_new_block(inode->i_sb);
 237         if (!tmp)
 238                 return NULL;
 239         result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
 240         if (*p) {
 241                 ext_free_block(inode->i_sb,tmp);
 242                 brelse(result);
 243                 goto repeat;
 244         }
 245         *p = tmp;
 246         inode->i_ctime = CURRENT_TIME;
 247         inode->i_dirt = 1;
 248         return result;
 249 }
 250 
 251 static struct buffer_head * block_getblk(struct inode * inode,
     /* [previous][next][first][last][top][bottom][index][help] */
 252         struct buffer_head * bh, int nr, int create)
 253 {
 254         int tmp;
 255         unsigned long * p;
 256         struct buffer_head * result;
 257 
 258         if (!bh)
 259                 return NULL;
 260         if (!bh->b_uptodate) {
 261                 ll_rw_block(READ, 1, &bh);
 262                 wait_on_buffer(bh);
 263                 if (!bh->b_uptodate) {
 264                         brelse(bh);
 265                         return NULL;
 266                 }
 267         }
 268         p = nr + (unsigned long *) bh->b_data;
 269 repeat:
 270         tmp = *p;
 271         if (tmp) {
 272                 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
 273                 if (tmp == *p) {
 274                         brelse(bh);
 275                         return result;
 276                 }
 277                 brelse(result);
 278                 goto repeat;
 279         }
 280         if (!create) {
 281                 brelse(bh);
 282                 return NULL;
 283         }
 284         tmp = ext_new_block(inode->i_sb);
 285         if (!tmp) {
 286                 brelse(bh);
 287                 return NULL;
 288         }
 289         result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
 290         if (*p) {
 291                 ext_free_block(inode->i_sb,tmp);
 292                 brelse(result);
 293                 goto repeat;
 294         }
 295         *p = tmp;
 296         bh->b_dirt = 1;
 297         brelse(bh);
 298         return result;
 299 }
 300 
 301 struct buffer_head * ext_getblk(struct inode * inode, int block, int create)
     /* [previous][next][first][last][top][bottom][index][help] */
 302 {
 303         struct buffer_head * bh;
 304 
 305         if (block<0) {
 306                 printk("ext_getblk: block<0\n");
 307                 return NULL;
 308         }
 309         if (block >= 9+256+256*256+256*256*256) {
 310                 printk("ext_getblk: block>big\n");
 311                 return NULL;
 312         }
 313         if (block<9)
 314                 return inode_getblk(inode,block,create);
 315         block -= 9;
 316         if (block<256) {
 317                 bh = inode_getblk(inode,9,create);
 318                 return block_getblk(inode,bh,block,create);
 319         }
 320         block -= 256;
 321         if (block<256*256) {
 322                 bh = inode_getblk(inode,10,create);
 323                 bh = block_getblk(inode,bh,block>>8,create);
 324                 return block_getblk(inode,bh,block & 255,create);
 325         }
 326         block -= 256*256;
 327         bh = inode_getblk(inode,11,create);
 328         bh = block_getblk(inode,bh,block>>16,create);
 329         bh = block_getblk(inode,bh,(block>>8) & 255,create);
 330         return block_getblk(inode,bh,block & 255,create);
 331 }
 332 
 333 struct buffer_head * ext_bread(struct inode * inode, int block, int create)
     /* [previous][next][first][last][top][bottom][index][help] */
 334 {
 335         struct buffer_head * bh;
 336 
 337         bh = ext_getblk(inode,block,create);
 338         if (!bh || bh->b_uptodate) 
 339                 return bh;
 340         ll_rw_block(READ, 1, &bh);
 341         wait_on_buffer(bh);
 342         if (bh->b_uptodate)
 343                 return bh;
 344         brelse(bh);
 345         return NULL;
 346 }
 347 
 348 void ext_read_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 349 {
 350         struct buffer_head * bh;
 351         struct ext_inode * raw_inode;
 352         int block;
 353 
 354         block = 2 + (inode->i_ino-1)/EXT_INODES_PER_BLOCK;
 355         if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE)))
 356                 panic("unable to read i-node block");
 357         raw_inode = ((struct ext_inode *) bh->b_data) +
 358                 (inode->i_ino-1)%EXT_INODES_PER_BLOCK;
 359         inode->i_mode = raw_inode->i_mode;
 360         inode->i_uid = raw_inode->i_uid;
 361         inode->i_gid = raw_inode->i_gid;
 362         inode->i_nlink = raw_inode->i_nlinks;
 363         inode->i_size = raw_inode->i_size;
 364         inode->i_mtime = inode->i_atime = inode->i_ctime = raw_inode->i_time;
 365         inode->i_blocks = inode->i_blksize = 0;
 366         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 367                 inode->i_rdev = raw_inode->i_zone[0];
 368         else for (block = 0; block < 12; block++)
 369                 inode->u.ext_i.i_data[block] = raw_inode->i_zone[block];
 370         brelse(bh);
 371         inode->i_op = NULL;
 372         if (S_ISREG(inode->i_mode))
 373                 inode->i_op = &ext_file_inode_operations;
 374         else if (S_ISDIR(inode->i_mode))
 375                 inode->i_op = &ext_dir_inode_operations;
 376         else if (S_ISLNK(inode->i_mode))
 377                 inode->i_op = &ext_symlink_inode_operations;
 378         else if (S_ISCHR(inode->i_mode))
 379                 inode->i_op = &chrdev_inode_operations;
 380         else if (S_ISBLK(inode->i_mode))
 381                 inode->i_op = &blkdev_inode_operations;
 382         else if (S_ISFIFO(inode->i_mode))
 383                 init_fifo(inode);
 384 }
 385 
 386 void ext_write_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 387 {
 388         struct buffer_head * bh;
 389         struct ext_inode * raw_inode;
 390         int block;
 391 
 392         block = 2 + (inode->i_ino-1)/EXT_INODES_PER_BLOCK;
 393         if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE)))
 394                 panic("unable to read i-node block");
 395         raw_inode = ((struct ext_inode *)bh->b_data) +
 396                 (inode->i_ino-1)%EXT_INODES_PER_BLOCK;
 397         raw_inode->i_mode = inode->i_mode;
 398         raw_inode->i_uid = inode->i_uid;
 399         raw_inode->i_gid = inode->i_gid;
 400         raw_inode->i_nlinks = inode->i_nlink;
 401         raw_inode->i_size = inode->i_size;
 402         raw_inode->i_time = inode->i_mtime;
 403         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 404                 raw_inode->i_zone[0] = inode->i_rdev;
 405         else for (block = 0; block < 12; block++)
 406                 raw_inode->i_zone[block] = inode->u.ext_i.i_data[block];
 407         bh->b_dirt=1;
 408         inode->i_dirt=0;
 409         brelse(bh);
 410 }

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