root/fs/ext2/super.c

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

DEFINITIONS

This source file includes following definitions.
  1. ext2_error
  2. ext2_panic
  3. ext2_warning
  4. ext2_put_super
  5. parse_options
  6. ext2_setup_super
  7. ext2_check_descriptors
  8. ext2_read_super
  9. ext2_commit_super
  10. ext2_write_super
  11. ext2_remount
  12. init_ext2_fs
  13. init_module
  14. cleanup_module
  15. ext2_statfs

   1 /*
   2  *  linux/fs/ext2/super.c
   3  *
   4  * Copyright (C) 1992, 1993, 1994, 1995
   5  * Remy Card (card@masi.ibp.fr)
   6  * Laboratoire MASI - Institut Blaise Pascal
   7  * Universite Pierre et Marie Curie (Paris VI)
   8  *
   9  *  from
  10  *
  11  *  linux/fs/minix/inode.c
  12  *
  13  *  Copyright (C) 1991, 1992  Linus Torvalds
  14  */
  15 
  16 #include <linux/module.h>
  17 
  18 #include <stdarg.h>
  19 
  20 #include <asm/bitops.h>
  21 #include <asm/segment.h>
  22 #include <asm/system.h>
  23 
  24 #include <linux/errno.h>
  25 #include <linux/fs.h>
  26 #include <linux/ext2_fs.h>
  27 #include <linux/malloc.h>
  28 #include <linux/sched.h>
  29 #include <linux/stat.h>
  30 #include <linux/string.h>
  31 #include <linux/locks.h>
  32 
  33 static char error_buf[1024];
  34 
  35 void ext2_error (struct super_block * sb, const char * function,
     /* [previous][next][first][last][top][bottom][index][help] */
  36                  const char * fmt, ...)
  37 {
  38         va_list args;
  39 
  40         if (!(sb->s_flags & MS_RDONLY)) {
  41                 sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
  42                 sb->u.ext2_sb.s_es->s_state |= EXT2_ERROR_FS;
  43                 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
  44                 sb->s_dirt = 1;
  45         }
  46         va_start (args, fmt);
  47         vsprintf (error_buf, fmt, args);
  48         va_end (args);
  49         if (test_opt (sb, ERRORS_PANIC) ||
  50             (sb->u.ext2_sb.s_es->s_errors == EXT2_ERRORS_PANIC &&
  51              !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_RO)))
  52                 panic ("EXT2-fs panic (device %s): %s: %s\n",
  53                        kdevname(sb->s_dev), function, error_buf);
  54         printk (KERN_CRIT "EXT2-fs error (device %s): %s: %s\n",
  55                 kdevname(sb->s_dev), function, error_buf);
  56         if (test_opt (sb, ERRORS_RO) ||
  57             (sb->u.ext2_sb.s_es->s_errors == EXT2_ERRORS_RO &&
  58              !test_opt (sb, ERRORS_CONT) && !test_opt (sb, ERRORS_PANIC))) {
  59                 printk ("Remounting filesystem read-only\n");
  60                 sb->s_flags |= MS_RDONLY;
  61         }
  62 }
  63 
  64 NORET_TYPE void ext2_panic (struct super_block * sb, const char * function,
     /* [previous][next][first][last][top][bottom][index][help] */
  65                             const char * fmt, ...)
  66 {
  67         va_list args;
  68 
  69         if (!(sb->s_flags & MS_RDONLY)) {
  70                 sb->u.ext2_sb.s_mount_state |= EXT2_ERROR_FS;
  71                 sb->u.ext2_sb.s_es->s_state |= EXT2_ERROR_FS;
  72                 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
  73                 sb->s_dirt = 1;
  74         }
  75         va_start (args, fmt);
  76         vsprintf (error_buf, fmt, args);
  77         va_end (args);
  78         /* this is to prevent panic from syncing this filesystem */
  79         if (sb->s_lock)
  80                 sb->s_lock=0;
  81         sb->s_flags |= MS_RDONLY;
  82         panic ("EXT2-fs panic (device %s): %s: %s\n",
  83                kdevname(sb->s_dev), function, error_buf);
  84 }
  85 
  86 void ext2_warning (struct super_block * sb, const char * function,
     /* [previous][next][first][last][top][bottom][index][help] */
  87                    const char * fmt, ...)
  88 {
  89         va_list args;
  90 
  91         va_start (args, fmt);
  92         vsprintf (error_buf, fmt, args);
  93         va_end (args);
  94         printk (KERN_WARNING "EXT2-fs warning (device %s): %s: %s\n",
  95                 kdevname(sb->s_dev), function, error_buf);
  96 }
  97 
  98 void ext2_put_super (struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  99 {
 100         int db_count;
 101         int i;
 102 
 103         lock_super (sb);
 104         if (!(sb->s_flags & MS_RDONLY)) {
 105                 sb->u.ext2_sb.s_es->s_state = sb->u.ext2_sb.s_mount_state;
 106                 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
 107         }
 108         sb->s_dev = 0;
 109         db_count = sb->u.ext2_sb.s_db_per_group;
 110         for (i = 0; i < db_count; i++)
 111                 if (sb->u.ext2_sb.s_group_desc[i])
 112                         brelse (sb->u.ext2_sb.s_group_desc[i]);
 113         kfree_s (sb->u.ext2_sb.s_group_desc,
 114                  db_count * sizeof (struct buffer_head *));
 115         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
 116                 if (sb->u.ext2_sb.s_inode_bitmap[i])
 117                         brelse (sb->u.ext2_sb.s_inode_bitmap[i]);
 118         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
 119                 if (sb->u.ext2_sb.s_block_bitmap[i])
 120                         brelse (sb->u.ext2_sb.s_block_bitmap[i]);
 121         brelse (sb->u.ext2_sb.s_sbh);
 122         unlock_super (sb);
 123         MOD_DEC_USE_COUNT;
 124         return;
 125 }
 126 
 127 static struct super_operations ext2_sops = { 
 128         ext2_read_inode,
 129         NULL,
 130         ext2_write_inode,
 131         ext2_put_inode,
 132         ext2_put_super,
 133         ext2_write_super,
 134         ext2_statfs,
 135         ext2_remount
 136 };
 137 
 138 /*
 139  * This function has been shamelessly adapted from the msdos fs
 140  */
 141 static int parse_options (char * options, unsigned long * sb_block,
     /* [previous][next][first][last][top][bottom][index][help] */
 142                           unsigned short *resuid, unsigned short * resgid,
 143                           unsigned long * mount_options)
 144 {
 145         char * this_char;
 146         char * value;
 147 
 148         if (!options)
 149                 return 1;
 150         for (this_char = strtok (options, ",");
 151              this_char != NULL;
 152              this_char = strtok (NULL, ",")) {
 153                 if ((value = strchr (this_char, '=')) != NULL)
 154                         *value++ = 0;
 155                 if (!strcmp (this_char, "bsddf"))
 156                         clear_opt (*mount_options, MINIX_DF);
 157                 else if (!strcmp (this_char, "check")) {
 158                         if (!value || !*value)
 159                                 set_opt (*mount_options, CHECK_NORMAL);
 160                         else if (!strcmp (value, "none")) {
 161                                 clear_opt (*mount_options, CHECK_NORMAL);
 162                                 clear_opt (*mount_options, CHECK_STRICT);
 163                         }
 164                         else if (!strcmp (value, "normal"))
 165                                 set_opt (*mount_options, CHECK_NORMAL);
 166                         else if (!strcmp (value, "strict")) {
 167                                 set_opt (*mount_options, CHECK_NORMAL);
 168                                 set_opt (*mount_options, CHECK_STRICT);
 169                         }
 170                         else {
 171                                 printk ("EXT2-fs: Invalid check option: %s\n",
 172                                         value);
 173                                 return 0;
 174                         }
 175                 }
 176                 else if (!strcmp (this_char, "debug"))
 177                         set_opt (*mount_options, DEBUG);
 178                 else if (!strcmp (this_char, "errors")) {
 179                         if (!value || !*value) {
 180                                 printk ("EXT2-fs: the errors option requires "
 181                                         "an argument");
 182                                 return 0;
 183                         }
 184                         if (!strcmp (value, "continue")) {
 185                                 clear_opt (*mount_options, ERRORS_RO);
 186                                 clear_opt (*mount_options, ERRORS_PANIC);
 187                                 set_opt (*mount_options, ERRORS_CONT);
 188                         }
 189                         else if (!strcmp (value, "remount-ro")) {
 190                                 clear_opt (*mount_options, ERRORS_CONT);
 191                                 clear_opt (*mount_options, ERRORS_PANIC);
 192                                 set_opt (*mount_options, ERRORS_RO);
 193                         }
 194                         else if (!strcmp (value, "panic")) {
 195                                 clear_opt (*mount_options, ERRORS_CONT);
 196                                 clear_opt (*mount_options, ERRORS_RO);
 197                                 set_opt (*mount_options, ERRORS_PANIC);
 198                         }
 199                         else {
 200                                 printk ("EXT2-fs: Invalid errors option: %s\n",
 201                                         value);
 202                                 return 0;
 203                         }
 204                 }
 205                 else if (!strcmp (this_char, "grpid") ||
 206                          !strcmp (this_char, "bsdgroups"))
 207                         set_opt (*mount_options, GRPID);
 208                 else if (!strcmp (this_char, "minixdf"))
 209                         set_opt (*mount_options, MINIX_DF);
 210                 else if (!strcmp (this_char, "nocheck")) {
 211                         clear_opt (*mount_options, CHECK_NORMAL);
 212                         clear_opt (*mount_options, CHECK_STRICT);
 213                 }
 214                 else if (!strcmp (this_char, "nogrpid") ||
 215                          !strcmp (this_char, "sysvgroups"))
 216                         clear_opt (*mount_options, GRPID);
 217                 else if (!strcmp (this_char, "resgid")) {
 218                         if (!value || !*value) {
 219                                 printk ("EXT2-fs: the resgid option requires "
 220                                         "an argument");
 221                                 return 0;
 222                         }
 223                         *resgid = simple_strtoul (value, &value, 0);
 224                         if (*value) {
 225                                 printk ("EXT2-fs: Invalid resgid option: %s\n",
 226                                         value);
 227                                 return 0;
 228                         }
 229                 }
 230                 else if (!strcmp (this_char, "resuid")) {
 231                         if (!value || !*value) {
 232                                 printk ("EXT2-fs: the resuid option requires "
 233                                         "an argument");
 234                                 return 0;
 235                         }
 236                         *resuid = simple_strtoul (value, &value, 0);
 237                         if (*value) {
 238                                 printk ("EXT2-fs: Invalid resuid option: %s\n",
 239                                         value);
 240                                 return 0;
 241                         }
 242                 }
 243                 else if (!strcmp (this_char, "sb")) {
 244                         if (!value || !*value) {
 245                                 printk ("EXT2-fs: the sb option requires "
 246                                         "an argument");
 247                                 return 0;
 248                         }
 249                         *sb_block = simple_strtoul (value, &value, 0);
 250                         if (*value) {
 251                                 printk ("EXT2-fs: Invalid sb option: %s\n",
 252                                         value);
 253                                 return 0;
 254                         }
 255                 }
 256                 /* Silently ignore the quota options */
 257                 else if (!strcmp (this_char, "grpquota")
 258                          || !strcmp (this_char, "noquota")
 259                          || !strcmp (this_char, "quota")
 260                          || !strcmp (this_char, "usrquota"))
 261                         /* Don't do anything ;-) */ ;
 262                 else {
 263                         printk ("EXT2-fs: Unrecognized mount option %s\n", this_char);
 264                         return 0;
 265                 }
 266         }
 267         return 1;
 268 }
 269 
 270 static void ext2_setup_super (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
 271                               struct ext2_super_block * es)
 272 {
 273         if (es->s_rev_level > EXT2_MAX_SUPP_REV) {
 274                         printk ("EXT2-fs warning: revision level too high, "
 275                                 "forcing read/only mode\n");
 276                         sb->s_flags |= MS_RDONLY;
 277         }
 278         if (!(sb->s_flags & MS_RDONLY)) {
 279                 if (!(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
 280                         printk ("EXT2-fs warning: mounting unchecked fs, "
 281                                 "running e2fsck is recommended\n");
 282                 else if ((sb->u.ext2_sb.s_mount_state & EXT2_ERROR_FS))
 283                         printk ("EXT2-fs warning: mounting fs with errors, "
 284                                 "running e2fsck is recommended\n");
 285                 else if (es->s_max_mnt_count >= 0 &&
 286                          es->s_mnt_count >= (unsigned short) es->s_max_mnt_count)
 287                         printk ("EXT2-fs warning: maximal mount count reached, "
 288                                 "running e2fsck is recommended\n");
 289                 else if (es->s_checkinterval &&
 290                         (es->s_lastcheck + es->s_checkinterval <= CURRENT_TIME))
 291                         printk ("EXT2-fs warning: checktime reached, "
 292                                 "running e2fsck is recommended\n");
 293                 es->s_state &= ~EXT2_VALID_FS;
 294                 if (!es->s_max_mnt_count)
 295                         es->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
 296                 es->s_mnt_count++;
 297                 es->s_mtime = CURRENT_TIME;
 298                 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
 299                 sb->s_dirt = 1;
 300                 if (test_opt (sb, DEBUG))
 301                         printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
 302                                 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
 303                                 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
 304                                 sb->u.ext2_sb.s_frag_size,
 305                                 sb->u.ext2_sb.s_groups_count,
 306                                 EXT2_BLOCKS_PER_GROUP(sb),
 307                                 EXT2_INODES_PER_GROUP(sb),
 308                                 sb->u.ext2_sb.s_mount_opt);
 309                 if (test_opt (sb, CHECK)) {
 310                         ext2_check_blocks_bitmap (sb);
 311                         ext2_check_inodes_bitmap (sb);
 312                 }
 313         }
 314 }
 315 
 316 static int ext2_check_descriptors (struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 317 {
 318         int i;
 319         int desc_block = 0;
 320         unsigned long block = sb->u.ext2_sb.s_es->s_first_data_block;
 321         struct ext2_group_desc * gdp = NULL;
 322 
 323         ext2_debug ("Checking group descriptors");
 324 
 325         for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++)
 326         {
 327                 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
 328                         gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[desc_block++]->b_data;
 329                 if (gdp->bg_block_bitmap < block ||
 330                     gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
 331                 {
 332                         ext2_error (sb, "ext2_check_descriptors",
 333                                     "Block bitmap for group %d"
 334                                     " not in group (block %lu)!",
 335                                     i, (unsigned long) gdp->bg_block_bitmap);
 336                         return 0;
 337                 }
 338                 if (gdp->bg_inode_bitmap < block ||
 339                     gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
 340                 {
 341                         ext2_error (sb, "ext2_check_descriptors",
 342                                     "Inode bitmap for group %d"
 343                                     " not in group (block %lu)!",
 344                                     i, (unsigned long) gdp->bg_inode_bitmap);
 345                         return 0;
 346                 }
 347                 if (gdp->bg_inode_table < block ||
 348                     gdp->bg_inode_table + sb->u.ext2_sb.s_itb_per_group >=
 349                     block + EXT2_BLOCKS_PER_GROUP(sb))
 350                 {
 351                         ext2_error (sb, "ext2_check_descriptors",
 352                                     "Inode table for group %d"
 353                                     " not in group (block %lu)!",
 354                                     i, (unsigned long) gdp->bg_inode_table);
 355                         return 0;
 356                 }
 357                 block += EXT2_BLOCKS_PER_GROUP(sb);
 358                 gdp++;
 359         }
 360         return 1;
 361 }
 362 
 363 #define log2(n) ffz(~(n))
 364 
 365 struct super_block * ext2_read_super (struct super_block * sb, void * data,
     /* [previous][next][first][last][top][bottom][index][help] */
 366                                       int silent)
 367 {
 368         struct buffer_head * bh;
 369         struct ext2_super_block * es;
 370         unsigned long sb_block = 1;
 371         unsigned short resuid = EXT2_DEF_RESUID;
 372         unsigned short resgid = EXT2_DEF_RESGID;
 373         unsigned long logic_sb_block = 1;
 374         kdev_t dev = sb->s_dev;
 375         int db_count;
 376         int i, j;
 377 
 378         set_opt (sb->u.ext2_sb.s_mount_opt, CHECK_NORMAL);
 379         if (!parse_options ((char *) data, &sb_block, &resuid, &resgid,
 380             &sb->u.ext2_sb.s_mount_opt)) {
 381                 sb->s_dev = 0;
 382                 return NULL;
 383         }
 384 
 385         MOD_INC_USE_COUNT;
 386         lock_super (sb);
 387         set_blocksize (dev, BLOCK_SIZE);
 388         if (!(bh = bread (dev, sb_block, BLOCK_SIZE))) {
 389                 sb->s_dev = 0;
 390                 unlock_super (sb);
 391                 printk ("EXT2-fs: unable to read superblock\n");
 392                 MOD_DEC_USE_COUNT;
 393                 return NULL;
 394         }
 395         /*
 396          * Note: s_es must be initialized s_es as soon as possible because
 397          * some ext2 macro-instructions depend on its value
 398          */
 399         es = (struct ext2_super_block *) bh->b_data;
 400         sb->u.ext2_sb.s_es = es;
 401         sb->s_magic = es->s_magic;
 402         if (sb->s_magic != EXT2_SUPER_MAGIC) {
 403                 if (!silent)
 404                         printk ("VFS: Can't find an ext2 filesystem on dev "
 405                                 "%s.\n", kdevname(dev));
 406         failed_mount:
 407                 sb->s_dev = 0;
 408                 unlock_super (sb);
 409                 if (bh)
 410                         brelse(bh);
 411                 MOD_DEC_USE_COUNT;
 412                 return NULL;
 413         }
 414         if ((es->s_rev_level > EXT2_GOOD_OLD_REV) &&
 415             (es->s_feature_incompat & !EXT2_FEATURE_INCOMPAT_SUPP)) {
 416                 printk("EXT2-fs: %s: couldn't mount because of "
 417                        "unsupported optional features.\n", kdevname(dev));
 418                 goto failed_mount;
 419         }
 420         sb->s_blocksize_bits = sb->u.ext2_sb.s_es->s_log_block_size + 10;
 421         sb->s_blocksize = 1 << sb->s_blocksize_bits;
 422         if (sb->s_blocksize != BLOCK_SIZE && 
 423             (sb->s_blocksize == 1024 || sb->s_blocksize == 2048 ||  
 424              sb->s_blocksize == 4096)) {
 425                 unsigned long offset;
 426 
 427                 brelse (bh);
 428                 set_blocksize (dev, sb->s_blocksize);
 429                 logic_sb_block = (sb_block*BLOCK_SIZE) / sb->s_blocksize;
 430                 offset = (sb_block*BLOCK_SIZE) % sb->s_blocksize;
 431                 bh = bread (dev, logic_sb_block, sb->s_blocksize);
 432                 if(!bh) {
 433                         printk("EXT2-fs: Couldn't read superblock on "
 434                                "2nd try.\n");
 435                         goto failed_mount;
 436                 }
 437                 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
 438                 sb->u.ext2_sb.s_es = es;
 439                 if (es->s_magic != EXT2_SUPER_MAGIC) {
 440                         printk ("EXT2-fs: Magic mismatch, very weird !\n");
 441                         goto failed_mount;
 442                 }
 443         }
 444         if (es->s_rev_level == EXT2_GOOD_OLD_REV) {
 445                 sb->u.ext2_sb.s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
 446                 sb->u.ext2_sb.s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
 447         } else {
 448                 sb->u.ext2_sb.s_inode_size = es->s_inode_size;
 449                 sb->u.ext2_sb.s_first_ino = es->s_first_ino;
 450                 if (sb->u.ext2_sb.s_inode_size != EXT2_GOOD_OLD_INODE_SIZE) {
 451                         printk ("EXT2-fs: unsupported inode size: %d\n",
 452                                 sb->u.ext2_sb.s_inode_size);
 453                         goto failed_mount;
 454                 }
 455         }
 456         sb->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
 457                                    es->s_log_frag_size;
 458         if (sb->u.ext2_sb.s_frag_size)
 459                 sb->u.ext2_sb.s_frags_per_block = sb->s_blocksize /
 460                                                   sb->u.ext2_sb.s_frag_size;
 461         else
 462                 sb->s_magic = 0;
 463         sb->u.ext2_sb.s_blocks_per_group = es->s_blocks_per_group;
 464         sb->u.ext2_sb.s_frags_per_group = es->s_frags_per_group;
 465         sb->u.ext2_sb.s_inodes_per_group = es->s_inodes_per_group;
 466         sb->u.ext2_sb.s_inodes_per_block = sb->s_blocksize /
 467                                            EXT2_INODE_SIZE(sb);
 468         sb->u.ext2_sb.s_itb_per_group = sb->u.ext2_sb.s_inodes_per_group /
 469                                         sb->u.ext2_sb.s_inodes_per_block;
 470         sb->u.ext2_sb.s_desc_per_block = sb->s_blocksize /
 471                                          sizeof (struct ext2_group_desc);
 472         sb->u.ext2_sb.s_sbh = bh;
 473         if (resuid != EXT2_DEF_RESUID)
 474                 sb->u.ext2_sb.s_resuid = resuid;
 475         else
 476                 sb->u.ext2_sb.s_resuid = es->s_def_resuid;
 477         if (resgid != EXT2_DEF_RESGID)
 478                 sb->u.ext2_sb.s_resgid = resgid;
 479         else
 480                 sb->u.ext2_sb.s_resgid = es->s_def_resgid;
 481         sb->u.ext2_sb.s_mount_state = es->s_state;
 482         sb->u.ext2_sb.s_rename_lock = 0;
 483         sb->u.ext2_sb.s_rename_wait = NULL;
 484         sb->u.ext2_sb.s_addr_per_block_bits =
 485                 log2 (EXT2_ADDR_PER_BLOCK(sb));
 486         sb->u.ext2_sb.s_desc_per_block_bits =
 487                 log2 (EXT2_DESC_PER_BLOCK(sb));
 488         if (sb->s_magic != EXT2_SUPER_MAGIC) {
 489                 if (!silent)
 490                         printk ("VFS: Can't find an ext2 filesystem on dev "
 491                                 "%s.\n",
 492                                 kdevname(dev));
 493                 goto failed_mount;
 494         }
 495         if (sb->s_blocksize != bh->b_size) {
 496                 if (!silent)
 497                         printk ("VFS: Unsupported blocksize on dev "
 498                                 "%s.\n", kdevname(dev));
 499                 goto failed_mount;
 500         }
 501 
 502         if (sb->s_blocksize != sb->u.ext2_sb.s_frag_size) {
 503                 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
 504                         sb->u.ext2_sb.s_frag_size, sb->s_blocksize);
 505                 goto failed_mount;
 506         }
 507 
 508         if (sb->u.ext2_sb.s_blocks_per_group > sb->s_blocksize * 8) {
 509                 printk ("EXT2-fs: #blocks per group too big: %lu\n",
 510                         sb->u.ext2_sb.s_blocks_per_group);
 511                 goto failed_mount;
 512         }
 513         if (sb->u.ext2_sb.s_frags_per_group > sb->s_blocksize * 8) {
 514                 printk ("EXT2-fs: #fragments per group too big: %lu\n",
 515                         sb->u.ext2_sb.s_frags_per_group);
 516                 goto failed_mount;
 517         }
 518         if (sb->u.ext2_sb.s_inodes_per_group > sb->s_blocksize * 8) {
 519                 printk ("EXT2-fs: #inodes per group too big: %lu\n",
 520                         sb->u.ext2_sb.s_inodes_per_group);
 521                 goto failed_mount;
 522         }
 523 
 524         sb->u.ext2_sb.s_groups_count = (es->s_blocks_count -
 525                                         es->s_first_data_block +
 526                                        EXT2_BLOCKS_PER_GROUP(sb) - 1) /
 527                                        EXT2_BLOCKS_PER_GROUP(sb);
 528         db_count = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
 529                    EXT2_DESC_PER_BLOCK(sb);
 530         sb->u.ext2_sb.s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
 531         if (sb->u.ext2_sb.s_group_desc == NULL) {
 532                 printk ("EXT2-fs: not enough memory\n");
 533                 goto failed_mount;
 534         }
 535         for (i = 0; i < db_count; i++) {
 536                 sb->u.ext2_sb.s_group_desc[i] = bread (dev, logic_sb_block + i + 1,
 537                                                        sb->s_blocksize);
 538                 if (!sb->u.ext2_sb.s_group_desc[i]) {
 539                         for (j = 0; j < i; j++)
 540                                 brelse (sb->u.ext2_sb.s_group_desc[j]);
 541                         kfree_s (sb->u.ext2_sb.s_group_desc,
 542                                  db_count * sizeof (struct buffer_head *));
 543                         printk ("EXT2-fs: unable to read group descriptors\n");
 544                         goto failed_mount;
 545                 }
 546         }
 547         if (!ext2_check_descriptors (sb)) {
 548                 for (j = 0; j < db_count; j++)
 549                         brelse (sb->u.ext2_sb.s_group_desc[j]);
 550                 kfree_s (sb->u.ext2_sb.s_group_desc,
 551                          db_count * sizeof (struct buffer_head *));
 552                 printk ("EXT2-fs: group descriptors corrupted !\n");
 553                 goto failed_mount;
 554         }
 555         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
 556                 sb->u.ext2_sb.s_inode_bitmap_number[i] = 0;
 557                 sb->u.ext2_sb.s_inode_bitmap[i] = NULL;
 558                 sb->u.ext2_sb.s_block_bitmap_number[i] = 0;
 559                 sb->u.ext2_sb.s_block_bitmap[i] = NULL;
 560         }
 561         sb->u.ext2_sb.s_loaded_inode_bitmaps = 0;
 562         sb->u.ext2_sb.s_loaded_block_bitmaps = 0;
 563         sb->u.ext2_sb.s_db_per_group = db_count;
 564         unlock_super (sb);
 565         /*
 566          * set up enough so that it can read an inode
 567          */
 568         sb->s_dev = dev;
 569         sb->s_op = &ext2_sops;
 570         if (!(sb->s_mounted = iget (sb, EXT2_ROOT_INO))) {
 571                 sb->s_dev = 0;
 572                 for (i = 0; i < db_count; i++)
 573                         if (sb->u.ext2_sb.s_group_desc[i])
 574                                 brelse (sb->u.ext2_sb.s_group_desc[i]);
 575                 kfree_s (sb->u.ext2_sb.s_group_desc,
 576                          db_count * sizeof (struct buffer_head *));
 577                 brelse (bh);
 578                 printk ("EXT2-fs: get root inode failed\n");
 579                 MOD_DEC_USE_COUNT;
 580                 return NULL;
 581         }
 582         ext2_setup_super (sb, es);
 583         return sb;
 584 }
 585 
 586 static void ext2_commit_super (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
 587                                struct ext2_super_block * es)
 588 {
 589         es->s_wtime = CURRENT_TIME;
 590         mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
 591         sb->s_dirt = 0;
 592 }
 593 
 594 /*
 595  * In the second extended file system, it is not necessary to
 596  * write the super block since we use a mapping of the
 597  * disk super block in a buffer.
 598  *
 599  * However, this function is still used to set the fs valid
 600  * flags to 0.  We need to set this flag to 0 since the fs
 601  * may have been checked while mounted and e2fsck may have
 602  * set s_state to EXT2_VALID_FS after some corrections.
 603  */
 604 
 605 void ext2_write_super (struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 606 {
 607         struct ext2_super_block * es;
 608 
 609         if (!(sb->s_flags & MS_RDONLY)) {
 610                 es = sb->u.ext2_sb.s_es;
 611 
 612                 ext2_debug ("setting valid to 0\n");
 613 
 614                 if (es->s_state & EXT2_VALID_FS) {
 615                         es->s_state &= ~EXT2_VALID_FS;
 616                         es->s_mtime = CURRENT_TIME;
 617                 }
 618                 ext2_commit_super (sb, es);
 619         }
 620         sb->s_dirt = 0;
 621 }
 622 
 623 int ext2_remount (struct super_block * sb, int * flags, char * data)
     /* [previous][next][first][last][top][bottom][index][help] */
 624 {
 625         struct ext2_super_block * es;
 626         unsigned short resuid = sb->u.ext2_sb.s_resuid;
 627         unsigned short resgid = sb->u.ext2_sb.s_resgid;
 628         unsigned long new_mount_opt;
 629         unsigned long tmp;
 630 
 631         /*
 632          * Allow the "check" option to be passed as a remount option.
 633          */
 634         new_mount_opt = EXT2_MOUNT_CHECK_NORMAL;
 635         if (!parse_options (data, &tmp, &resuid, &resgid,
 636                             &new_mount_opt))
 637                 return -EINVAL;
 638 
 639         sb->u.ext2_sb.s_mount_opt = new_mount_opt;
 640         sb->u.ext2_sb.s_resuid = resuid;
 641         sb->u.ext2_sb.s_resgid = resgid;
 642         es = sb->u.ext2_sb.s_es;
 643         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
 644                 return 0;
 645         if (*flags & MS_RDONLY) {
 646                 if (es->s_state & EXT2_VALID_FS ||
 647                     !(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
 648                         return 0;
 649                 /*
 650                  * OK, we are remounting a valid rw partition rdonly, so set
 651                  * the rdonly flag and then mark the partition as valid again.
 652                  */
 653                 es->s_state = sb->u.ext2_sb.s_mount_state;
 654                 es->s_mtime = CURRENT_TIME;
 655                 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
 656                 sb->s_dirt = 1;
 657                 ext2_commit_super (sb, es);
 658         }
 659         else {
 660                 /*
 661                  * Mounting a RDONLY partition read-write, so reread and
 662                  * store the current valid flag.  (It may have been changed 
 663                  * by e2fsck since we originally mounted the partition.)
 664                  */
 665                 sb->u.ext2_sb.s_mount_state = es->s_state;
 666                 sb->s_flags &= ~MS_RDONLY;
 667                 ext2_setup_super (sb, es);
 668         }
 669         return 0;
 670 }
 671 
 672 static struct file_system_type ext2_fs_type = {
 673         ext2_read_super, "ext2", 1, NULL
 674 };
 675 
 676 int init_ext2_fs(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 677 {
 678         return register_filesystem(&ext2_fs_type);
 679 }
 680 
 681 #ifdef MODULE
 682 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 683 {
 684         int status;
 685 
 686         if ((status = init_ext2_fs()) == 0)
 687                 register_symtab(0);
 688         return status;
 689 }
 690 
 691 void cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 692 {
 693         unregister_filesystem(&ext2_fs_type);
 694 }
 695 
 696 #endif
 697 
 698 void ext2_statfs (struct super_block * sb, struct statfs * buf, int bufsiz)
     /* [previous][next][first][last][top][bottom][index][help] */
 699 {
 700         unsigned long overhead;
 701         unsigned long overhead_per_group;
 702         struct statfs tmp;
 703 
 704         if (test_opt (sb, MINIX_DF))
 705                 overhead = 0;
 706         else {
 707                 /*
 708                  * Compute the overhead (FS structures)
 709                  */
 710                 overhead_per_group = 1 /* super block */ +
 711                                      sb->u.ext2_sb.s_db_per_group /* descriptors */ +
 712                                      1 /* block bitmap */ +
 713                                      1 /* inode bitmap */ +
 714                                      sb->u.ext2_sb.s_itb_per_group /* inode table */;
 715                 overhead = sb->u.ext2_sb.s_es->s_first_data_block +
 716                            sb->u.ext2_sb.s_groups_count * overhead_per_group;
 717         }
 718 
 719         tmp.f_type = EXT2_SUPER_MAGIC;
 720         tmp.f_bsize = sb->s_blocksize;
 721         tmp.f_blocks = sb->u.ext2_sb.s_es->s_blocks_count - overhead;
 722         tmp.f_bfree = ext2_count_free_blocks (sb);
 723         tmp.f_bavail = tmp.f_bfree - sb->u.ext2_sb.s_es->s_r_blocks_count;
 724         if (tmp.f_bfree < sb->u.ext2_sb.s_es->s_r_blocks_count)
 725                 tmp.f_bavail = 0;
 726         tmp.f_files = sb->u.ext2_sb.s_es->s_inodes_count;
 727         tmp.f_ffree = ext2_count_free_inodes (sb);
 728         tmp.f_namelen = EXT2_NAME_LEN;
 729         memcpy_tofs(buf, &tmp, bufsiz);
 730 }

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