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 long * mount_options)
 177 {
 178         char * this_char;
 179         char * value;
 180 
 181         if (!options)
 182                 return 1;
 183         for (this_char = strtok (options, ",");
 184              this_char != NULL;
 185              this_char = strtok (NULL, ",")) {
 186                 if ((value = strchr (this_char, '=')) != NULL)
 187                         *value++ = 0;
 188                 if (!strcmp (this_char, "bsddf"))
 189                         clear_opt (*mount_options, MINIX_DF);
 190                 else if (!strcmp (this_char, "check")) {
 191                         if (!value || !*value)
 192                                 set_opt (*mount_options, CHECK_NORMAL);
 193                         else if (!strcmp (value, "none")) {
 194                                 clear_opt (*mount_options, CHECK_NORMAL);
 195                                 clear_opt (*mount_options, CHECK_STRICT);
 196                         }
 197                         else if (strcmp (value, "normal"))
 198                                 set_opt (*mount_options, CHECK_NORMAL);
 199                         else if (strcmp (value, "strict")) {
 200                                 set_opt (*mount_options, CHECK_NORMAL);
 201                                 set_opt (*mount_options, CHECK_STRICT);
 202                         }
 203                         else {
 204                                 printk ("EXT2-fs: Invalid check option: %s\n",
 205                                         value);
 206                                 return 0;
 207                         }
 208                 }
 209                 else if (!strcmp (this_char, "debug"))
 210                         set_opt (*mount_options, DEBUG);
 211                 else if (!strcmp (this_char, "errors")) {
 212                         if (!value || !*value) {
 213                                 printk ("EXT2-fs: the errors option requires "
 214                                         "an argument");
 215                                 return 0;
 216                         }
 217                         if (!strcmp (value, "continue")) {
 218                                 clear_opt (*mount_options, ERRORS_RO);
 219                                 clear_opt (*mount_options, ERRORS_PANIC);
 220                                 set_opt (*mount_options, ERRORS_CONT);
 221                         }
 222                         else if (!strcmp (value, "remount-ro")) {
 223                                 clear_opt (*mount_options, ERRORS_CONT);
 224                                 clear_opt (*mount_options, ERRORS_PANIC);
 225                                 set_opt (*mount_options, ERRORS_RO);
 226                         }
 227                         else if (!strcmp (value, "panic")) {
 228                                 clear_opt (*mount_options, ERRORS_CONT);
 229                                 clear_opt (*mount_options, ERRORS_RO);
 230                                 set_opt (*mount_options, ERRORS_PANIC);
 231                         }
 232                         else {
 233                                 printk ("EXT2-fs: Invalid errors option: %s\n",
 234                                         value);
 235                                 return 0;
 236                         }
 237                 }
 238                 else if (!strcmp (this_char, "grpid") ||
 239                          !strcmp (this_char, "bsdgroups"))
 240                         set_opt (*mount_options, GRPID);
 241                 else if (!strcmp (this_char, "minixdf"))
 242                         set_opt (*mount_options, MINIX_DF);
 243                 else if (!strcmp (this_char, "nocheck")) {
 244                         clear_opt (*mount_options, CHECK_NORMAL);
 245                         clear_opt (*mount_options, CHECK_STRICT);
 246                 }
 247                 else if (!strcmp (this_char, "nogrpid") ||
 248                          !strcmp (this_char, "sysvgroups"))
 249                         clear_opt (*mount_options, GRPID);
 250                 else if (!strcmp (this_char, "sb")) {
 251                         if (!value || !*value) {
 252                                 printk ("EXT2-fs: the sb option requires "
 253                                         "an argument");
 254                                 return 0;
 255                         }
 256                         *sb_block = simple_strtoul (value, &value, 0);
 257                         if (*value) {
 258                                 printk ("EXT2-fs: Invalid sb option: %s\n",
 259                                         value);
 260                                 return 0;
 261                         }
 262                 }
 263                 else {
 264                         printk ("EXT2-fs: Unrecognized mount option %s\n", this_char);
 265                         return 0;
 266                 }
 267         }
 268         return 1;
 269 }
 270 
 271 static void ext2_setup_super (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
 272                               struct ext2_super_block * es)
 273 {
 274         if (!(sb->s_flags & MS_RDONLY)) {
 275                 if (!(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
 276                         printk ("EXT2-fs warning: mounting unchecked fs, "
 277                                 "running e2fsck is recommended\n");
 278                 else if ((sb->u.ext2_sb.s_mount_state & EXT2_ERROR_FS))
 279                         printk ("EXT2-fs warning: mounting fs with errors, "
 280                                 "running e2fsck is recommended\n");
 281                 else if (es->s_max_mnt_count >= 0 &&
 282                          es->s_mnt_count >= (unsigned short) es->s_max_mnt_count)
 283                         printk ("EXT2-fs warning: maximal mount count reached, "
 284                                 "running e2fsck is recommended\n");
 285                 else if (es->s_checkinterval &&
 286                         (es->s_lastcheck + es->s_checkinterval <= CURRENT_TIME))
 287                         printk ("EXT2-fs warning: checktime reached, "
 288                                 "running e2fsck is recommended\n");
 289                 es->s_state &= ~EXT2_VALID_FS;
 290                 if (!es->s_max_mnt_count)
 291                         es->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
 292                 es->s_mnt_count++;
 293                 es->s_mtime = CURRENT_TIME;
 294                 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
 295                 sb->s_dirt = 1;
 296                 if (test_opt (sb, DEBUG))
 297                         printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
 298                                 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
 299                                 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
 300                                 sb->u.ext2_sb.s_frag_size,
 301                                 sb->u.ext2_sb.s_groups_count,
 302                                 EXT2_BLOCKS_PER_GROUP(sb),
 303                                 EXT2_INODES_PER_GROUP(sb),
 304                                 sb->u.ext2_sb.s_mount_opt);
 305                 if (test_opt (sb, CHECK)) {
 306                         ext2_check_blocks_bitmap (sb);
 307                         ext2_check_inodes_bitmap (sb);
 308                 }
 309         }
 310 }
 311 
 312 static int ext2_check_descriptors (struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 313 {
 314         int i;
 315         int desc_block = 0;
 316         unsigned long block = sb->u.ext2_sb.s_es->s_first_data_block;
 317         struct ext2_group_desc * gdp = NULL;
 318 
 319         ext2_debug ("Checking group descriptors");
 320 
 321         for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++)
 322         {
 323                 if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
 324                         gdp = (struct ext2_group_desc *) sb->u.ext2_sb.s_group_desc[desc_block++]->b_data;
 325                 if (gdp->bg_block_bitmap < block ||
 326                     gdp->bg_block_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
 327                 {
 328                         ext2_error (sb, "ext2_check_descriptors",
 329                                     "Block bitmap for group %d"
 330                                     " not in group (block %lu)!",
 331                                     i, gdp->bg_block_bitmap);
 332                         return 0;
 333                 }
 334                 if (gdp->bg_inode_bitmap < block ||
 335                     gdp->bg_inode_bitmap >= block + EXT2_BLOCKS_PER_GROUP(sb))
 336                 {
 337                         ext2_error (sb, "ext2_check_descriptors",
 338                                     "Inode bitmap for group %d"
 339                                     " not in group (block %lu)!",
 340                                     i, gdp->bg_inode_bitmap);
 341                         return 0;
 342                 }
 343                 if (gdp->bg_inode_table < block ||
 344                     gdp->bg_inode_table + sb->u.ext2_sb.s_itb_per_group >=
 345                     block + EXT2_BLOCKS_PER_GROUP(sb))
 346                 {
 347                         ext2_error (sb, "ext2_check_descriptors",
 348                                     "Inode table for group %d"
 349                                     " not in group (block %lu)!",
 350                                     i, gdp->bg_inode_table);
 351                         return 0;
 352                 }
 353                 block += EXT2_BLOCKS_PER_GROUP(sb);
 354                 gdp++;
 355         }
 356         return 1;
 357 }
 358 
 359 struct super_block * ext2_read_super (struct super_block * sb, void * data,
     /* [previous][next][first][last][top][bottom][index][help] */
 360                                       int silent)
 361 {
 362         struct buffer_head * bh;
 363         struct ext2_super_block * es;
 364         unsigned long sb_block = 1;
 365         unsigned long logic_sb_block = 1;
 366         int dev = sb->s_dev;
 367         int db_count;
 368         int i, j;
 369 #ifdef EXT2FS_PRE_02B_COMPAT
 370         int fs_converted = 0;
 371 #endif
 372 
 373         set_opt (sb->u.ext2_sb.s_mount_opt, CHECK_NORMAL);
 374         if (!parse_options ((char *) data, &sb_block,
 375             &sb->u.ext2_sb.s_mount_opt)) {
 376                 sb->s_dev = 0;
 377                 return NULL;
 378         }
 379 
 380         lock_super (sb);
 381         set_blocksize (dev, BLOCK_SIZE);
 382         if (!(bh = bread (dev, sb_block, BLOCK_SIZE))) {
 383                 sb->s_dev = 0;
 384                 unlock_super (sb);
 385                 printk ("EXT2-fs: unable to read superblock\n");
 386                 return NULL;
 387         }
 388         /*
 389          * Note: s_es must be initialized s_es as soon as possible because
 390          * some ext2 macro-instructions depend on its value
 391          */
 392         es = (struct ext2_super_block *) bh->b_data;
 393         sb->u.ext2_sb.s_es = es;
 394         sb->s_magic = es->s_magic;
 395         if (sb->s_magic != EXT2_SUPER_MAGIC
 396 #ifdef EXT2FS_PRE_02B_COMPAT
 397            && sb->s_magic != EXT2_PRE_02B_MAGIC
 398 #endif
 399            ) {
 400                 sb->s_dev = 0;
 401                 unlock_super (sb);
 402                 brelse (bh);
 403                 if (!silent)
 404                         printk ("VFS: Can't find an ext2 filesystem on dev %d/%d.\n",
 405                                 MAJOR(dev), MINOR(dev));
 406                 return NULL;
 407         }
 408         sb->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
 409         sb->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(sb);
 410         if (sb->s_blocksize != BLOCK_SIZE && 
 411             (sb->s_blocksize == 1024 || sb->s_blocksize == 2048 ||  
 412              sb->s_blocksize == 4096)) {
 413                 unsigned long offset;
 414 
 415                 brelse (bh);
 416                 set_blocksize (dev, sb->s_blocksize);
 417                 logic_sb_block = (sb_block*BLOCK_SIZE) / sb->s_blocksize;
 418                 offset = (sb_block*BLOCK_SIZE) % sb->s_blocksize;
 419                 bh = bread (dev, logic_sb_block, sb->s_blocksize);
 420                 if(!bh)
 421                         return NULL;
 422                 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
 423                 sb->u.ext2_sb.s_es = es;
 424                 if (es->s_magic != EXT2_SUPER_MAGIC) {
 425                         sb->s_dev = 0;
 426                         unlock_super (sb);
 427                         brelse (bh);
 428                         printk ("EXT2-fs: Magic mismatch, very weird !\n");
 429                         return NULL;
 430                 }
 431         }
 432         sb->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
 433                                    es->s_log_frag_size;
 434         if (sb->u.ext2_sb.s_frag_size)
 435                 sb->u.ext2_sb.s_frags_per_block = sb->s_blocksize /
 436                                                   sb->u.ext2_sb.s_frag_size;
 437         else
 438                 sb->s_magic = 0;
 439         sb->u.ext2_sb.s_blocks_per_group = es->s_blocks_per_group;
 440         sb->u.ext2_sb.s_frags_per_group = es->s_frags_per_group;
 441         sb->u.ext2_sb.s_inodes_per_group = es->s_inodes_per_group;
 442         sb->u.ext2_sb.s_inodes_per_block = sb->s_blocksize /
 443                                            sizeof (struct ext2_inode);
 444         sb->u.ext2_sb.s_itb_per_group = sb->u.ext2_sb.s_inodes_per_group /
 445                                         sb->u.ext2_sb.s_inodes_per_block;
 446         sb->u.ext2_sb.s_desc_per_block = sb->s_blocksize /
 447                                          sizeof (struct ext2_group_desc);
 448         sb->u.ext2_sb.s_sbh = bh;
 449         sb->u.ext2_sb.s_es = es;
 450         sb->u.ext2_sb.s_mount_state = es->s_state;
 451         sb->u.ext2_sb.s_rename_lock = 0;
 452         sb->u.ext2_sb.s_rename_wait = NULL;
 453 #ifdef EXT2FS_PRE_02B_COMPAT
 454         if (sb->s_magic == EXT2_PRE_02B_MAGIC) {
 455                 if (es->s_blocks_count > 262144) {
 456                         /*
 457                          * fs > 256 MB can't be converted
 458                          */ 
 459                         sb->s_dev = 0;
 460                         unlock_super (sb);
 461                         brelse (bh);
 462                         printk ("EXT2-fs: trying to mount a pre-0.2b file"
 463                                 "system which cannot be converted\n");
 464                         return NULL;
 465                 }
 466                 printk ("EXT2-fs: mounting a pre 0.2b file system, "
 467                         "will try to convert the structure\n");
 468                 if (!(sb->s_flags & MS_RDONLY)) {
 469                         sb->s_dev = 0;
 470                         unlock_super (sb);
 471                         brelse (bh);
 472                         printk ("EXT2-fs: cannot convert a read-only fs\n");
 473                         return NULL;
 474                 }
 475                 if (!convert_pre_02b_fs (sb, bh)) {
 476                         sb->s_dev = 0;
 477                         unlock_super (sb);
 478                         brelse (bh);
 479                         printk ("EXT2-fs: conversion failed !!!\n");
 480                         return NULL;
 481                 }
 482                 printk ("EXT2-fs: conversion succeeded !!!\n");
 483                 fs_converted = 1;
 484         }
 485 #endif
 486         if (sb->s_magic != EXT2_SUPER_MAGIC) {
 487                 sb->s_dev = 0;
 488                 unlock_super (sb);
 489                 brelse (bh);
 490                 if (!silent)
 491                         printk ("VFS: Can't find an ext2 filesystem on dev %d/%d.\n",
 492                                 MAJOR(dev), MINOR(dev));
 493                 return NULL;
 494         }
 495         if (sb->s_blocksize != bh->b_size) {
 496                 sb->s_dev = 0;
 497                 unlock_super (sb);
 498                 brelse (bh);
 499                 if (!silent)
 500                         printk ("VFS: Unsupported blocksize on dev 0x%04x.\n",
 501                                 dev);
 502                 return NULL;
 503         }
 504 
 505         if (sb->s_blocksize != sb->u.ext2_sb.s_frag_size) {
 506                 sb->s_dev = 0;
 507                 unlock_super (sb);
 508                 brelse (bh);
 509                 printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
 510                         sb->u.ext2_sb.s_frag_size, sb->s_blocksize);
 511                 return NULL;
 512         }
 513 
 514         sb->u.ext2_sb.s_groups_count = (es->s_blocks_count -
 515                                         es->s_first_data_block +
 516                                        EXT2_BLOCKS_PER_GROUP(sb) - 1) /
 517                                        EXT2_BLOCKS_PER_GROUP(sb);
 518         db_count = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
 519                    EXT2_DESC_PER_BLOCK(sb);
 520         sb->u.ext2_sb.s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
 521         if (sb->u.ext2_sb.s_group_desc == NULL) {
 522                 sb->s_dev = 0;
 523                 unlock_super (sb);
 524                 brelse (bh);
 525                 printk ("EXT2-fs: not enough memory\n");
 526                 return NULL;
 527         }
 528         for (i = 0; i < db_count; i++) {
 529                 sb->u.ext2_sb.s_group_desc[i] = bread (dev, logic_sb_block + i + 1,
 530                                                        sb->s_blocksize);
 531                 if (!sb->u.ext2_sb.s_group_desc[i]) {
 532                         sb->s_dev = 0;
 533                         unlock_super (sb);
 534                         for (j = 0; j < i; j++)
 535                                 brelse (sb->u.ext2_sb.s_group_desc[j]);
 536                         kfree_s (sb->u.ext2_sb.s_group_desc,
 537                                  db_count * sizeof (struct buffer_head *));
 538                         brelse (bh);
 539                         printk ("EXT2-fs: unable to read group descriptors\n");
 540                         return NULL;
 541                 }
 542         }
 543         if (!ext2_check_descriptors (sb)) {
 544                 sb->s_dev = 0;
 545                 unlock_super (sb);
 546                 for (j = 0; j < db_count; j++)
 547                         brelse (sb->u.ext2_sb.s_group_desc[j]);
 548                 kfree_s (sb->u.ext2_sb.s_group_desc,
 549                          db_count * sizeof (struct buffer_head *));
 550                 brelse (bh);
 551                 printk ("EXT2-fs: group descriptors corrupted !\n");
 552                 return NULL;
 553         }
 554         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
 555                 sb->u.ext2_sb.s_inode_bitmap_number[i] = 0;
 556                 sb->u.ext2_sb.s_inode_bitmap[i] = NULL;
 557                 sb->u.ext2_sb.s_block_bitmap_number[i] = 0;
 558                 sb->u.ext2_sb.s_block_bitmap[i] = NULL;
 559         }
 560         sb->u.ext2_sb.s_loaded_inode_bitmaps = 0;
 561         sb->u.ext2_sb.s_loaded_block_bitmaps = 0;
 562         sb->u.ext2_sb.s_db_per_group = db_count;
 563         unlock_super (sb);
 564         /*
 565          * set up enough so that it can read an inode
 566          */
 567         sb->s_dev = dev;
 568         sb->s_op = &ext2_sops;
 569         if (!(sb->s_mounted = iget (sb, EXT2_ROOT_INO))) {
 570                 sb->s_dev = 0;
 571                 for (i = 0; i < db_count; i++)
 572                         if (sb->u.ext2_sb.s_group_desc[i])
 573                                 brelse (sb->u.ext2_sb.s_group_desc[i]);
 574                 kfree_s (sb->u.ext2_sb.s_group_desc,
 575                          db_count * sizeof (struct buffer_head *));
 576                 brelse (bh);
 577                 printk ("EXT2-fs: get root inode failed\n");
 578                 return NULL;
 579         }
 580 #ifdef EXT2FS_PRE_02B_COMPAT
 581         if (fs_converted) {
 582                 for (i = 0; i < db_count; i++)
 583                         mark_buffer_dirty(sb->u.ext2_sb.s_group_desc[i], 1);
 584                 sb->s_dirt = 1;
 585         }
 586 #endif
 587         ext2_setup_super (sb, es);
 588         return sb;
 589 }
 590 
 591 static void ext2_commit_super (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
 592                                struct ext2_super_block * es)
 593 {
 594         es->s_wtime = CURRENT_TIME;
 595         mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
 596         sb->s_dirt = 0;
 597 }
 598 
 599 /*
 600  * In the second extended file system, it is not necessary to
 601  * write the super block since we use a mapping of the
 602  * disk super block in a buffer.
 603  *
 604  * However, this function is still used to set the fs valid
 605  * flags to 0.  We need to set this flag to 0 since the fs
 606  * may have been checked while mounted and e2fsck may have
 607  * set s_state to EXT2_VALID_FS after some corrections.
 608  */
 609 
 610 void ext2_write_super (struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 611 {
 612         struct ext2_super_block * es;
 613 
 614         if (!(sb->s_flags & MS_RDONLY)) {
 615                 es = sb->u.ext2_sb.s_es;
 616 
 617                 ext2_debug ("setting valid to 0\n");
 618 
 619                 if (es->s_state & EXT2_VALID_FS) {
 620                         es->s_state &= ~EXT2_VALID_FS;
 621                         es->s_mtime = CURRENT_TIME;
 622                 }
 623                 ext2_commit_super (sb, es);
 624         }
 625         sb->s_dirt = 0;
 626 }
 627 
 628 int ext2_remount (struct super_block * sb, int * flags, char * data)
     /* [previous][next][first][last][top][bottom][index][help] */
 629 {
 630         struct ext2_super_block * es;
 631         unsigned long tmp;
 632 
 633         /*
 634          * Allow the "check" option to be passed as a remount option.
 635          */
 636         set_opt (sb->u.ext2_sb.s_mount_opt, CHECK_NORMAL);
 637         parse_options (data, &tmp, &sb->u.ext2_sb.s_mount_opt);
 638 
 639         es = sb->u.ext2_sb.s_es;
 640         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
 641                 return 0;
 642         if (*flags & MS_RDONLY) {
 643                 if (es->s_state & EXT2_VALID_FS ||
 644                     !(sb->u.ext2_sb.s_mount_state & EXT2_VALID_FS))
 645                         return 0;
 646                 /*
 647                  * OK, we are remounting a valid rw partition rdonly, so set
 648                  * the rdonly flag and then mark the partition as valid again.
 649                  */
 650                 es->s_state = sb->u.ext2_sb.s_mount_state;
 651                 es->s_mtime = CURRENT_TIME;
 652                 mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
 653                 sb->s_dirt = 1;
 654                 ext2_commit_super (sb, es);
 655         }
 656         else {
 657                 /*
 658                  * Mounting a RDONLY partition read-write, so reread and
 659                  * store the current valid flag.  (It may have been changed 
 660                  * by e2fsck since we originally mounted the partition.)
 661                  */
 662                 sb->u.ext2_sb.s_mount_state = es->s_state;
 663                 sb->s_flags &= ~MS_RDONLY;
 664                 ext2_setup_super (sb, es);
 665         }
 666         return 0;
 667 }
 668 
 669 void ext2_statfs (struct super_block * sb, struct statfs * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 670 {
 671         long tmp;
 672         unsigned long overhead;
 673         unsigned long overhead_per_group;
 674 
 675         if (test_opt (sb, MINIX_DF))
 676                 overhead = 0;
 677         else {
 678                 /*
 679                  * Compute the overhead (FS structures)
 680                  */
 681                 overhead_per_group = 1 /* super block */ +
 682                                      sb->u.ext2_sb.s_db_per_group /* descriptors */ +
 683                                      1 /* block bitmap */ +
 684                                      1 /* inode bitmap */ +
 685                                      sb->u.ext2_sb.s_itb_per_group /* inode table */;
 686                 overhead = sb->u.ext2_sb.s_es->s_first_data_block +
 687                            sb->u.ext2_sb.s_groups_count * overhead_per_group;
 688         }
 689 
 690         put_fs_long (EXT2_SUPER_MAGIC, &buf->f_type);
 691         put_fs_long (sb->s_blocksize, &buf->f_bsize);
 692         put_fs_long (sb->u.ext2_sb.s_es->s_blocks_count - overhead,
 693                      &buf->f_blocks);
 694         tmp = ext2_count_free_blocks (sb);
 695         put_fs_long (tmp, &buf->f_bfree);
 696         if (tmp >= sb->u.ext2_sb.s_es->s_r_blocks_count)
 697                 put_fs_long (tmp - sb->u.ext2_sb.s_es->s_r_blocks_count,
 698                              &buf->f_bavail);
 699         else
 700                 put_fs_long (0, &buf->f_bavail);
 701         put_fs_long (sb->u.ext2_sb.s_es->s_inodes_count, &buf->f_files);
 702         put_fs_long (ext2_count_free_inodes (sb), &buf->f_ffree);
 703         put_fs_long (EXT2_NAME_LEN, &buf->f_namelen);
 704         /* Don't know what value to put in buf->f_fsid */
 705 }

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