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

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