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

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