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_commit_super
  3. minix_write_super
  4. minix_put_super
  5. minix_remount
  6. minix_read_super
  7. minix_statfs
  8. block_bmap
  9. minix_bmap
  10. inode_getblk
  11. block_getblk
  12. minix_getblk
  13. minix_bread
  14. minix_read_inode
  15. minix_update_inode
  16. minix_write_inode
  17. minix_sync_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 #include <linux/locks.h>
  14 
  15 #include <asm/system.h>
  16 #include <asm/segment.h>
  17 #include <asm/bitops.h>
  18 
  19 void minix_put_inode(struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  20 {
  21         if (inode->i_nlink)
  22                 return;
  23         inode->i_size = 0;
  24         minix_truncate(inode);
  25         minix_free_inode(inode);
  26 }
  27 
  28 static void minix_commit_super (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
  29                                struct minix_super_block * ms)
  30 {
  31         mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
  32         sb->s_dirt = 0;
  33 }
  34 
  35 void minix_write_super (struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  36 {
  37         struct minix_super_block * ms;
  38 
  39         if (!(sb->s_flags & MS_RDONLY)) {
  40                 ms = sb->u.minix_sb.s_ms;
  41 
  42                 if (ms->s_state & MINIX_VALID_FS)
  43                         ms->s_state &= ~MINIX_VALID_FS;
  44                 minix_commit_super (sb, ms);
  45         }
  46         sb->s_dirt = 0;
  47 }
  48 
  49 
  50 void minix_put_super(struct super_block *sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52         int i;
  53 
  54         lock_super(sb);
  55         if (!(sb->s_flags & MS_RDONLY)) {
  56                 sb->u.minix_sb.s_ms->s_state = sb->u.minix_sb.s_mount_state;
  57                 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
  58         }
  59         sb->s_dev = 0;
  60         for(i = 0 ; i < MINIX_I_MAP_SLOTS ; i++)
  61                 brelse(sb->u.minix_sb.s_imap[i]);
  62         for(i = 0 ; i < MINIX_Z_MAP_SLOTS ; i++)
  63                 brelse(sb->u.minix_sb.s_zmap[i]);
  64         brelse (sb->u.minix_sb.s_sbh);
  65         unlock_super(sb);
  66         return;
  67 }
  68 
  69 static struct super_operations minix_sops = { 
  70         minix_read_inode,
  71         NULL,
  72         minix_write_inode,
  73         minix_put_inode,
  74         minix_put_super,
  75         minix_write_super,
  76         minix_statfs,
  77         minix_remount
  78 };
  79 
  80 int minix_remount (struct super_block * sb, int * flags, char * data)
     /* [previous][next][first][last][top][bottom][index][help] */
  81 {
  82         struct minix_super_block * ms;
  83 
  84         ms = sb->u.minix_sb.s_ms;
  85         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  86                 return 0;
  87         if (*flags & MS_RDONLY) {
  88                 if (ms->s_state & MINIX_VALID_FS ||
  89                     !(sb->u.minix_sb.s_mount_state & MINIX_VALID_FS))
  90                         return 0;
  91                 /* Mounting a rw partition read-only. */
  92                 ms->s_state = sb->u.minix_sb.s_mount_state;
  93                 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
  94                 sb->s_dirt = 1;
  95                 minix_commit_super (sb, ms);
  96         }
  97         else {
  98                 /* Mount a partition which is read-only, read-write. */
  99                 sb->u.minix_sb.s_mount_state = ms->s_state;
 100                 ms->s_state &= ~MINIX_VALID_FS;
 101                 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
 102                 sb->s_dirt = 1;
 103 
 104                 if (!(sb->u.minix_sb.s_mount_state & MINIX_VALID_FS))
 105                         printk ("MINIX-fs warning: remounting unchecked fs, "
 106                                 "running fsck is recommended.\n");
 107                 else if ((sb->u.minix_sb.s_mount_state & MINIX_ERROR_FS))
 108                         printk ("MINIX-fs warning: remounting fs with errors, "
 109                                 "running fsck is recommended.\n");
 110         }
 111         return 0;
 112 }
 113 
 114 
 115 struct super_block *minix_read_super(struct super_block *s,void *data, 
     /* [previous][next][first][last][top][bottom][index][help] */
 116                                      int silent)
 117 {
 118         struct buffer_head *bh;
 119         struct minix_super_block *ms;
 120         int i,dev=s->s_dev,block;
 121 
 122         if (32 != sizeof (struct minix_inode))
 123                 panic("bad i-node size");
 124         lock_super(s);
 125         set_blocksize(dev, BLOCK_SIZE);
 126         if (!(bh = bread(dev,1,BLOCK_SIZE))) {
 127                 s->s_dev=0;
 128                 unlock_super(s);
 129                 printk("MINIX-fs: unable to read superblock\n");
 130                 return NULL;
 131         }
 132         ms = (struct minix_super_block *) bh->b_data;
 133         s->u.minix_sb.s_ms = ms;
 134         s->u.minix_sb.s_sbh = bh;
 135         s->u.minix_sb.s_mount_state = ms->s_state;
 136         s->s_blocksize = 1024;
 137         s->s_blocksize_bits = 10;
 138         s->u.minix_sb.s_ninodes = ms->s_ninodes;
 139         s->u.minix_sb.s_nzones = ms->s_nzones;
 140         s->u.minix_sb.s_imap_blocks = ms->s_imap_blocks;
 141         s->u.minix_sb.s_zmap_blocks = ms->s_zmap_blocks;
 142         s->u.minix_sb.s_firstdatazone = ms->s_firstdatazone;
 143         s->u.minix_sb.s_log_zone_size = ms->s_log_zone_size;
 144         s->u.minix_sb.s_max_size = ms->s_max_size;
 145         s->s_magic = ms->s_magic;
 146         if (s->s_magic == MINIX_SUPER_MAGIC) {
 147                 s->u.minix_sb.s_dirsize = 16;
 148                 s->u.minix_sb.s_namelen = 14;
 149         } else if (s->s_magic == MINIX_SUPER_MAGIC2) {
 150                 s->u.minix_sb.s_dirsize = 32;
 151                 s->u.minix_sb.s_namelen = 30;
 152         } else {
 153                 s->s_dev = 0;
 154                 unlock_super(s);
 155                 brelse(bh);
 156                 if (!silent)
 157                         printk("VFS: Can't find a minix filesystem on dev 0x%04x.\n", dev);
 158                 return NULL;
 159         }
 160         for (i=0;i < MINIX_I_MAP_SLOTS;i++)
 161                 s->u.minix_sb.s_imap[i] = NULL;
 162         for (i=0;i < MINIX_Z_MAP_SLOTS;i++)
 163                 s->u.minix_sb.s_zmap[i] = NULL;
 164         block=2;
 165         for (i=0 ; i < s->u.minix_sb.s_imap_blocks ; i++)
 166                 if ((s->u.minix_sb.s_imap[i]=bread(dev,block,BLOCK_SIZE)) != NULL)
 167                         block++;
 168                 else
 169                         break;
 170         for (i=0 ; i < s->u.minix_sb.s_zmap_blocks ; i++)
 171                 if ((s->u.minix_sb.s_zmap[i]=bread(dev,block,BLOCK_SIZE)) != NULL)
 172                         block++;
 173                 else
 174                         break;
 175         if (block != 2+s->u.minix_sb.s_imap_blocks+s->u.minix_sb.s_zmap_blocks) {
 176                 for(i=0;i<MINIX_I_MAP_SLOTS;i++)
 177                         brelse(s->u.minix_sb.s_imap[i]);
 178                 for(i=0;i<MINIX_Z_MAP_SLOTS;i++)
 179                         brelse(s->u.minix_sb.s_zmap[i]);
 180                 s->s_dev=0;
 181                 unlock_super(s);
 182                 brelse(bh);
 183                 printk("MINIX-fs: bad superblock or unable to read bitmaps\n");
 184                 return NULL;
 185         }
 186         set_bit(0,s->u.minix_sb.s_imap[0]->b_data);
 187         set_bit(0,s->u.minix_sb.s_zmap[0]->b_data);
 188         unlock_super(s);
 189         /* set up enough so that it can read an inode */
 190         s->s_dev = dev;
 191         s->s_op = &minix_sops;
 192         s->s_mounted = iget(s,MINIX_ROOT_INO);
 193         if (!s->s_mounted) {
 194                 s->s_dev = 0;
 195                 brelse(bh);
 196                 printk("MINIX-fs: get root inode failed\n");
 197                 return NULL;
 198         }
 199         if (!(s->s_flags & MS_RDONLY)) {
 200                 ms->s_state &= ~MINIX_VALID_FS;
 201                 mark_buffer_dirty(bh, 1);
 202                 s->s_dirt = 1;
 203         }
 204         if (!(s->u.minix_sb.s_mount_state & MINIX_VALID_FS))
 205                 printk ("MINIX-fs: mounting unchecked file system, "
 206                         "running fsck is recommended.\n");
 207         else if (s->u.minix_sb.s_mount_state & MINIX_ERROR_FS)
 208                 printk ("MINIX-fs: mounting file system with errors, "
 209                         "running fsck is recommended.\n");
 210         return s;
 211 }
 212 
 213 void minix_statfs(struct super_block *sb, struct statfs *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 214 {
 215         long tmp;
 216 
 217         put_fs_long(MINIX_SUPER_MAGIC, &buf->f_type);
 218         put_fs_long(1024, &buf->f_bsize);
 219         tmp = sb->u.minix_sb.s_nzones - sb->u.minix_sb.s_firstdatazone;
 220         tmp <<= sb->u.minix_sb.s_log_zone_size;
 221         put_fs_long(tmp, &buf->f_blocks);
 222         tmp = minix_count_free_blocks(sb);
 223         put_fs_long(tmp, &buf->f_bfree);
 224         put_fs_long(tmp, &buf->f_bavail);
 225         put_fs_long(sb->u.minix_sb.s_ninodes, &buf->f_files);
 226         put_fs_long(minix_count_free_inodes(sb), &buf->f_ffree);
 227         put_fs_long(sb->u.minix_sb.s_namelen, &buf->f_namelen);
 228         /* Don't know what value to put in buf->f_fsid */
 229 }
 230 
 231 #define inode_bmap(inode,nr) ((inode)->u.minix_i.i_data[(nr)])
 232 
 233 static int block_bmap(struct buffer_head * bh, int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235         int tmp;
 236 
 237         if (!bh)
 238                 return 0;
 239         tmp = ((unsigned short *) bh->b_data)[nr];
 240         brelse(bh);
 241         return tmp;
 242 }
 243 
 244 int minix_bmap(struct inode * inode,int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 245 {
 246         int i;
 247 
 248         if (block<0) {
 249                 printk("minix_bmap: block<0");
 250                 return 0;
 251         }
 252         if (block >= 7+512+512*512) {
 253                 printk("minix_bmap: block>big");
 254                 return 0;
 255         }
 256         if (block < 7)
 257                 return inode_bmap(inode,block);
 258         block -= 7;
 259         if (block < 512) {
 260                 i = inode_bmap(inode,7);
 261                 if (!i)
 262                         return 0;
 263                 return block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block);
 264         }
 265         block -= 512;
 266         i = inode_bmap(inode,8);
 267         if (!i)
 268                 return 0;
 269         i = block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block>>9);
 270         if (!i)
 271                 return 0;
 272         return block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block & 511);
 273 }
 274 
 275 static struct buffer_head * inode_getblk(struct inode * inode, int nr, int create)
     /* [previous][next][first][last][top][bottom][index][help] */
 276 {
 277         int tmp;
 278         unsigned short *p;
 279         struct buffer_head * result;
 280 
 281         p = inode->u.minix_i.i_data + nr;
 282 repeat:
 283         tmp = *p;
 284         if (tmp) {
 285                 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
 286                 if (tmp == *p)
 287                         return result;
 288                 brelse(result);
 289                 goto repeat;
 290         }
 291         if (!create)
 292                 return NULL;
 293         tmp = minix_new_block(inode->i_sb);
 294         if (!tmp)
 295                 return NULL;
 296         result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
 297         if (*p) {
 298                 minix_free_block(inode->i_sb,tmp);
 299                 brelse(result);
 300                 goto repeat;
 301         }
 302         *p = tmp;
 303         inode->i_ctime = CURRENT_TIME;
 304         inode->i_dirt = 1;
 305         return result;
 306 }
 307 
 308 static struct buffer_head * block_getblk(struct inode * inode, 
     /* [previous][next][first][last][top][bottom][index][help] */
 309         struct buffer_head * bh, int nr, int create)
 310 {
 311         int tmp;
 312         unsigned short *p;
 313         struct buffer_head * result;
 314 
 315         if (!bh)
 316                 return NULL;
 317         if (!bh->b_uptodate) {
 318                 ll_rw_block(READ, 1, &bh);
 319                 wait_on_buffer(bh);
 320                 if (!bh->b_uptodate) {
 321                         brelse(bh);
 322                         return NULL;
 323                 }
 324         }
 325         p = nr + (unsigned short *) bh->b_data;
 326 repeat:
 327         tmp = *p;
 328         if (tmp) {
 329                 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
 330                 if (tmp == *p) {
 331                         brelse(bh);
 332                         return result;
 333                 }
 334                 brelse(result);
 335                 goto repeat;
 336         }
 337         if (!create) {
 338                 brelse(bh);
 339                 return NULL;
 340         }
 341         tmp = minix_new_block(inode->i_sb);
 342         if (!tmp) {
 343                 brelse(bh);
 344                 return NULL;
 345         }
 346         result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
 347         if (*p) {
 348                 minix_free_block(inode->i_sb,tmp);
 349                 brelse(result);
 350                 goto repeat;
 351         }
 352         *p = tmp;
 353         mark_buffer_dirty(bh, 1);
 354         brelse(bh);
 355         return result;
 356 }
 357 
 358 struct buffer_head * minix_getblk(struct inode * inode, int block, int create)
     /* [previous][next][first][last][top][bottom][index][help] */
 359 {
 360         struct buffer_head * bh;
 361 
 362         if (block<0) {
 363                 printk("minix_getblk: block<0");
 364                 return NULL;
 365         }
 366         if (block >= 7+512+512*512) {
 367                 printk("minix_getblk: block>big");
 368                 return NULL;
 369         }
 370         if (block < 7)
 371                 return inode_getblk(inode,block,create);
 372         block -= 7;
 373         if (block < 512) {
 374                 bh = inode_getblk(inode,7,create);
 375                 return block_getblk(inode, bh, block, create);
 376         }
 377         block -= 512;
 378         bh = inode_getblk(inode,8,create);
 379         bh = block_getblk(inode, bh, block>>9, create);
 380         return block_getblk(inode, bh, block & 511, create);
 381 }
 382 
 383 struct buffer_head * minix_bread(struct inode * inode, int block, int create)
     /* [previous][next][first][last][top][bottom][index][help] */
 384 {
 385         struct buffer_head * bh;
 386 
 387         bh = minix_getblk(inode,block,create);
 388         if (!bh || bh->b_uptodate)
 389                 return bh;
 390         ll_rw_block(READ, 1, &bh);
 391         wait_on_buffer(bh);
 392         if (bh->b_uptodate)
 393                 return bh;
 394         brelse(bh);
 395         return NULL;
 396 }
 397 
 398 void minix_read_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 399 {
 400         struct buffer_head * bh;
 401         struct minix_inode * raw_inode;
 402         int block, ino;
 403 
 404         ino = inode->i_ino;
 405         inode->i_op = NULL;
 406         inode->i_mode = 0;
 407         if (!ino || ino >= inode->i_sb->u.minix_sb.s_ninodes) {
 408                 printk("Bad inode number on dev 0x%04x: %d is out of range\n",
 409                         inode->i_dev, ino);
 410                 return;
 411         }
 412         block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks +
 413                     inode->i_sb->u.minix_sb.s_zmap_blocks +
 414                     (ino-1)/MINIX_INODES_PER_BLOCK;
 415         if (!(bh=bread(inode->i_dev,block, BLOCK_SIZE))) {
 416                 printk("Major problem: unable to read inode from dev 0x%04x\n",
 417                         inode->i_dev);
 418                 return;
 419         }
 420         raw_inode = ((struct minix_inode *) bh->b_data) +
 421                     (ino-1)%MINIX_INODES_PER_BLOCK;
 422         inode->i_mode = raw_inode->i_mode;
 423         inode->i_uid = raw_inode->i_uid;
 424         inode->i_gid = raw_inode->i_gid;
 425         inode->i_nlink = raw_inode->i_nlinks;
 426         inode->i_size = raw_inode->i_size;
 427         inode->i_mtime = inode->i_atime = inode->i_ctime = raw_inode->i_time;
 428         inode->i_blocks = inode->i_blksize = 0;
 429         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 430                 inode->i_rdev = raw_inode->i_zone[0];
 431         else for (block = 0; block < 9; block++)
 432                 inode->u.minix_i.i_data[block] = raw_inode->i_zone[block];
 433         brelse(bh);
 434         if (S_ISREG(inode->i_mode))
 435                 inode->i_op = &minix_file_inode_operations;
 436         else if (S_ISDIR(inode->i_mode))
 437                 inode->i_op = &minix_dir_inode_operations;
 438         else if (S_ISLNK(inode->i_mode))
 439                 inode->i_op = &minix_symlink_inode_operations;
 440         else if (S_ISCHR(inode->i_mode))
 441                 inode->i_op = &chrdev_inode_operations;
 442         else if (S_ISBLK(inode->i_mode))
 443                 inode->i_op = &blkdev_inode_operations;
 444         else if (S_ISFIFO(inode->i_mode))
 445                 init_fifo(inode);
 446 }
 447 
 448 static struct buffer_head * minix_update_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 449 {
 450         struct buffer_head * bh;
 451         struct minix_inode * raw_inode;
 452         int ino, block;
 453 
 454         ino = inode->i_ino;
 455         if (!ino || ino >= inode->i_sb->u.minix_sb.s_ninodes) {
 456                 printk("Bad inode number on dev 0x%04x: %d is out of range\n",
 457                         inode->i_dev, ino);
 458                 inode->i_dirt = 0;
 459                 return 0;
 460         }
 461         block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
 462                 (ino-1)/MINIX_INODES_PER_BLOCK;
 463         if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE))) {
 464                 printk("unable to read i-node block\n");
 465                 inode->i_dirt = 0;
 466                 return 0;
 467         }
 468         raw_inode = ((struct minix_inode *)bh->b_data) +
 469                 (ino-1)%MINIX_INODES_PER_BLOCK;
 470         raw_inode->i_mode = inode->i_mode;
 471         raw_inode->i_uid = inode->i_uid;
 472         raw_inode->i_gid = inode->i_gid;
 473         raw_inode->i_nlinks = inode->i_nlink;
 474         raw_inode->i_size = inode->i_size;
 475         raw_inode->i_time = inode->i_mtime;
 476         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 477                 raw_inode->i_zone[0] = inode->i_rdev;
 478         else for (block = 0; block < 9; block++)
 479                 raw_inode->i_zone[block] = inode->u.minix_i.i_data[block];
 480         inode->i_dirt=0;
 481         mark_buffer_dirty(bh, 1);
 482         return bh;
 483 }
 484 
 485 void minix_write_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 486 {
 487         struct buffer_head *bh;
 488         bh = minix_update_inode(inode);
 489         brelse(bh);
 490 }
 491 
 492 int minix_sync_inode(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 493 {
 494         int err = 0;
 495         struct buffer_head *bh;
 496 
 497         bh = minix_update_inode(inode);
 498         if (bh && bh->b_dirt)
 499         {
 500                 ll_rw_block(WRITE, 1, &bh);
 501                 wait_on_buffer(bh);
 502                 if (bh->b_req && !bh->b_uptodate)
 503                 {
 504                         printk ("IO error syncing minix inode [%04x:%08lx]\n",
 505                                 inode->i_dev, inode->i_ino);
 506                         err = -1;
 507                 }
 508         }
 509         else if (!bh)
 510                 err = -1;
 511         brelse (bh);
 512         return err;
 513 }

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