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
  18. init_module
  19. cleanup_module

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

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