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

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