root/fs/ext2/balloc.c

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

DEFINITIONS

This source file includes following definitions.
  1. get_group_desc
  2. read_block_bitmap
  3. load__block_bitmap
  4. load_block_bitmap
  5. ext2_free_blocks
  6. ext2_new_block
  7. ext2_count_free_blocks
  8. block_in_use
  9. ext2_check_blocks_bitmap

   1 /*
   2  *  linux/fs/ext2/balloc.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  *  Enhanced block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
  10  */
  11 
  12 /*
  13  * balloc.c contains the blocks allocation and deallocation routines
  14  */
  15 
  16 /*
  17  * The free blocks are managed by bitmaps.  A file system contains several
  18  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
  19  * block for inodes, N blocks for the inode table and data blocks.
  20  *
  21  * The file system contains group descriptors which are located after the
  22  * super block.  Each descriptor contains the number of the bitmap block and
  23  * the free blocks count in the block.  The descriptors are loaded in memory
  24  * when a file system is mounted (see ext2_read_super).
  25  */
  26 
  27 #include <linux/fs.h>
  28 #include <linux/ext2_fs.h>
  29 #include <linux/stat.h>
  30 #include <linux/sched.h>
  31 #include <linux/string.h>
  32 #include <linux/locks.h>
  33 
  34 #include <asm/bitops.h>
  35 
  36 #define in_range(b, first, len)         ((b) >= (first) && (b) <= (first) + (len) - 1)
  37 
  38 static struct ext2_group_desc * get_group_desc (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
  39                                                 unsigned int block_group,
  40                                                 struct buffer_head ** bh)
  41 {
  42         unsigned long group_desc;
  43         unsigned long desc;
  44         struct ext2_group_desc * gdp;
  45 
  46         if (block_group >= sb->u.ext2_sb.s_groups_count)
  47                 ext2_panic (sb, "get_group_desc",
  48                             "block_group >= groups_count - "
  49                             "block_group = %d, groups_count = %lu",
  50                             block_group, sb->u.ext2_sb.s_groups_count);
  51 
  52         group_desc = block_group / EXT2_DESC_PER_BLOCK(sb);
  53         desc = block_group % EXT2_DESC_PER_BLOCK(sb);
  54         if (!sb->u.ext2_sb.s_group_desc[group_desc])
  55                 ext2_panic (sb, "get_group_desc",
  56                             "Group descriptor not loaded - "
  57                             "block_group = %d, group_desc = %lu, desc = %lu",
  58                              block_group, group_desc, desc);
  59         gdp = (struct ext2_group_desc *) 
  60               sb->u.ext2_sb.s_group_desc[group_desc]->b_data;
  61         if (bh)
  62                 *bh = sb->u.ext2_sb.s_group_desc[group_desc];
  63         return gdp + desc;
  64 }
  65 
  66 static void read_block_bitmap (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
  67                                unsigned int block_group,
  68                                unsigned long bitmap_nr)
  69 {
  70         struct ext2_group_desc * gdp;
  71         struct buffer_head * bh;
  72         
  73         gdp = get_group_desc (sb, block_group, NULL);
  74         bh = bread (sb->s_dev, gdp->bg_block_bitmap, sb->s_blocksize);
  75         if (!bh)
  76                 ext2_panic (sb, "read_block_bitmap",
  77                             "Cannot read block bitmap - "
  78                             "block_group = %d, block_bitmap = %lu",
  79                             block_group, (unsigned long) gdp->bg_block_bitmap);
  80         sb->u.ext2_sb.s_block_bitmap_number[bitmap_nr] = block_group;
  81         sb->u.ext2_sb.s_block_bitmap[bitmap_nr] = bh;
  82 }
  83 
  84 /*
  85  * load_block_bitmap loads the block bitmap for a blocks group
  86  *
  87  * It maintains a cache for the last bitmaps loaded.  This cache is managed
  88  * with a LRU algorithm.
  89  *
  90  * Notes:
  91  * 1/ There is one cache per mounted file system.
  92  * 2/ If the file system contains less than EXT2_MAX_GROUP_LOADED groups,
  93  *    this function reads the bitmap without maintaining a LRU cache.
  94  */
  95 static int load__block_bitmap (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
  96                                unsigned int block_group)
  97 {
  98         int i, j;
  99         unsigned long block_bitmap_number;
 100         struct buffer_head * block_bitmap;
 101 
 102         if (block_group >= sb->u.ext2_sb.s_groups_count)
 103                 ext2_panic (sb, "load_block_bitmap",
 104                             "block_group >= groups_count - "
 105                             "block_group = %d, groups_count = %lu",
 106                             block_group, sb->u.ext2_sb.s_groups_count);
 107 
 108         if (sb->u.ext2_sb.s_groups_count <= EXT2_MAX_GROUP_LOADED) {
 109                 if (sb->u.ext2_sb.s_block_bitmap[block_group]) {
 110                         if (sb->u.ext2_sb.s_block_bitmap_number[block_group] !=
 111                             block_group)
 112                                 ext2_panic (sb, "load_block_bitmap",
 113                                             "block_group != block_bitmap_number");
 114                         else
 115                                 return block_group;
 116                 } else {
 117                         read_block_bitmap (sb, block_group, block_group);
 118                         return block_group;
 119                 }
 120         }
 121 
 122         for (i = 0; i < sb->u.ext2_sb.s_loaded_block_bitmaps &&
 123                     sb->u.ext2_sb.s_block_bitmap_number[i] != block_group; i++)
 124                 ;
 125         if (i < sb->u.ext2_sb.s_loaded_block_bitmaps &&
 126             sb->u.ext2_sb.s_block_bitmap_number[i] == block_group) {
 127                 block_bitmap_number = sb->u.ext2_sb.s_block_bitmap_number[i];
 128                 block_bitmap = sb->u.ext2_sb.s_block_bitmap[i];
 129                 for (j = i; j > 0; j--) {
 130                         sb->u.ext2_sb.s_block_bitmap_number[j] =
 131                                 sb->u.ext2_sb.s_block_bitmap_number[j - 1];
 132                         sb->u.ext2_sb.s_block_bitmap[j] =
 133                                 sb->u.ext2_sb.s_block_bitmap[j - 1];
 134                 }
 135                 sb->u.ext2_sb.s_block_bitmap_number[0] = block_bitmap_number;
 136                 sb->u.ext2_sb.s_block_bitmap[0] = block_bitmap;
 137         } else {
 138                 if (sb->u.ext2_sb.s_loaded_block_bitmaps < EXT2_MAX_GROUP_LOADED)
 139                         sb->u.ext2_sb.s_loaded_block_bitmaps++;
 140                 else
 141                         brelse (sb->u.ext2_sb.s_block_bitmap[EXT2_MAX_GROUP_LOADED - 1]);
 142                 for (j = sb->u.ext2_sb.s_loaded_block_bitmaps - 1; j > 0;  j--) {
 143                         sb->u.ext2_sb.s_block_bitmap_number[j] =
 144                                 sb->u.ext2_sb.s_block_bitmap_number[j - 1];
 145                         sb->u.ext2_sb.s_block_bitmap[j] =
 146                                 sb->u.ext2_sb.s_block_bitmap[j - 1];
 147                 }
 148                 read_block_bitmap (sb, block_group, 0);
 149         }
 150         return 0;
 151 }
 152 
 153 static inline int load_block_bitmap (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
 154                                      unsigned int block_group)
 155 {
 156         if (sb->u.ext2_sb.s_loaded_block_bitmaps > 0 &&
 157             sb->u.ext2_sb.s_block_bitmap_number[0] == block_group)
 158                 return 0;
 159         
 160         if (sb->u.ext2_sb.s_groups_count <= EXT2_MAX_GROUP_LOADED && 
 161             sb->u.ext2_sb.s_block_bitmap_number[block_group] == block_group &&
 162             sb->u.ext2_sb.s_block_bitmap[block_group]) 
 163                 return block_group;
 164 
 165         return load__block_bitmap (sb, block_group);
 166 }
 167 
 168 void ext2_free_blocks (const struct inode * inode, unsigned long block,
     /* [previous][next][first][last][top][bottom][index][help] */
 169                        unsigned long count)
 170 {
 171         struct buffer_head * bh;
 172         struct buffer_head * bh2;
 173         unsigned long block_group;
 174         unsigned long bit;
 175         unsigned long i;
 176         int bitmap_nr;
 177         struct super_block * sb;
 178         struct ext2_group_desc * gdp;
 179         struct ext2_super_block * es;
 180 
 181         sb = inode->i_sb;
 182         if (!sb) {
 183                 printk ("ext2_free_blocks: nonexistent device");
 184                 return;
 185         }
 186         lock_super (sb);
 187         es = sb->u.ext2_sb.s_es;
 188         if (block < es->s_first_data_block || 
 189             (block + count) > es->s_blocks_count) {
 190                 ext2_error (sb, "ext2_free_blocks",
 191                             "Freeing blocks not in datazone - "
 192                             "block = %lu, count = %lu", block, count);
 193                 unlock_super (sb);
 194                 return;
 195         }
 196 
 197         ext2_debug ("freeing block %lu\n", block);
 198 
 199         block_group = (block - es->s_first_data_block) /
 200                       EXT2_BLOCKS_PER_GROUP(sb);
 201         bit = (block - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb);
 202         if (bit + count > EXT2_BLOCKS_PER_GROUP(sb))
 203                 ext2_panic (sb, "ext2_free_blocks",
 204                             "Freeing blocks across group boundary - "
 205                             "Block = %lu, count = %lu",
 206                             block, count);
 207         bitmap_nr = load_block_bitmap (sb, block_group);
 208         bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
 209         gdp = get_group_desc (sb, block_group, &bh2);
 210 
 211         if (test_opt (sb, CHECK_STRICT) &&
 212             (in_range (gdp->bg_block_bitmap, block, count) ||
 213              in_range (gdp->bg_inode_bitmap, block, count) ||
 214              in_range (block, gdp->bg_inode_table,
 215                        sb->u.ext2_sb.s_itb_per_group) ||
 216              in_range (block + count - 1, gdp->bg_inode_table,
 217                        sb->u.ext2_sb.s_itb_per_group)))
 218                 ext2_panic (sb, "ext2_free_blocks",
 219                             "Freeing blocks in system zones - "
 220                             "Block = %lu, count = %lu",
 221                             block, count);
 222 
 223         for (i = 0; i < count; i++) {
 224                 if (!clear_bit (bit + i, bh->b_data))
 225                         ext2_warning (sb, "ext2_free_blocks",
 226                                       "bit already cleared for block %lu", 
 227                                       block);
 228                 else {
 229                         if (sb->dq_op)
 230                                 sb->dq_op->free_block(inode, fs_to_dq_blocks(1, sb->s_blocksize));
 231                         gdp->bg_free_blocks_count++;
 232                         es->s_free_blocks_count++;
 233                 }
 234         }
 235         
 236         mark_buffer_dirty(bh2, 1);
 237         mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
 238 
 239         mark_buffer_dirty(bh, 1);
 240         if (sb->s_flags & MS_SYNCHRONOUS) {
 241                 ll_rw_block (WRITE, 1, &bh);
 242                 wait_on_buffer (bh);
 243         }
 244         sb->s_dirt = 1;
 245         unlock_super (sb);
 246         return;
 247 }
 248 
 249 /*
 250  * ext2_new_block uses a goal block to assist allocation.  If the goal is
 251  * free, or there is a free block within 32 blocks of the goal, that block
 252  * is allocated.  Otherwise a forward search is made for a free block; within 
 253  * each block group the search first looks for an entire free byte in the block
 254  * bitmap, and then for any free bit if that fails.
 255  */
 256 int ext2_new_block (const struct inode * inode, unsigned long goal,
     /* [previous][next][first][last][top][bottom][index][help] */
 257                     u32 * prealloc_count, u32 * prealloc_block, int * err)
 258 {
 259         struct buffer_head * bh;
 260         struct buffer_head * bh2;
 261         char * p, * r;
 262         int i, j, k, tmp;
 263         int bitmap_nr;
 264         struct super_block * sb;
 265         struct ext2_group_desc * gdp;
 266         struct ext2_super_block * es;
 267 
 268         *err = -ENOSPC;
 269 #ifdef EXT2FS_DEBUG
 270         static int goal_hits = 0, goal_attempts = 0;
 271 #endif
 272         sb = inode->i_sb;
 273         if (!sb) {
 274                 printk ("ext2_new_block: nonexistent device");
 275                 return 0;
 276         }
 277         lock_super (sb);
 278         es = sb->u.ext2_sb.s_es;
 279         if (es->s_free_blocks_count <= es->s_r_blocks_count &&
 280             (!fsuser() && (sb->u.ext2_sb.s_resuid != current->fsuid) &&
 281              (sb->u.ext2_sb.s_resgid == 0 ||
 282               !in_group_p (sb->u.ext2_sb.s_resgid)))) {
 283                 unlock_super (sb);
 284                 return 0;
 285         }
 286 
 287         ext2_debug ("goal=%lu.\n", goal);
 288 
 289 repeat:
 290         /*
 291          * First, test whether the goal block is free.
 292          */
 293         if (goal < es->s_first_data_block || goal >= es->s_blocks_count)
 294                 goal = es->s_first_data_block;
 295         i = (goal - es->s_first_data_block) / EXT2_BLOCKS_PER_GROUP(sb);
 296         gdp = get_group_desc (sb, i, &bh2);
 297         if (gdp->bg_free_blocks_count > 0) {
 298                 j = ((goal - es->s_first_data_block) % EXT2_BLOCKS_PER_GROUP(sb));
 299 #ifdef EXT2FS_DEBUG
 300                 if (j)
 301                         goal_attempts++;
 302 #endif
 303                 bitmap_nr = load_block_bitmap (sb, i);
 304                 bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
 305 
 306                 ext2_debug ("goal is at %d:%d.\n", i, j);
 307 
 308                 if (!test_bit(j, bh->b_data)) {
 309 #ifdef EXT2FS_DEBUG
 310                         goal_hits++;
 311                         ext2_debug ("goal bit allocated.\n");
 312 #endif
 313                         goto got_block;
 314                 }
 315                 if (j) {
 316                         /*
 317                          * The goal was occupied; search forward for a free 
 318                          * block within the next XX blocks.
 319                          *
 320                          * end_goal is more or less random, but it has to be
 321                          * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
 322                          * next 64-bit boundary is simple..
 323                          */
 324                         int end_goal = (j + 63) & ~63;
 325                         j = find_next_zero_bit(bh->b_data, end_goal, j);
 326                         if (j < end_goal)
 327                                 goto got_block;
 328                 }
 329         
 330                 ext2_debug ("Bit not found near goal\n");
 331 
 332                 /*
 333                  * There has been no free block found in the near vicinity
 334                  * of the goal: do a search forward through the block groups,
 335                  * searching in each group first for an entire free byte in
 336                  * the bitmap and then for any free bit.
 337                  * 
 338                  * Search first in the remainder of the current group; then,
 339                  * cyclicly search through the rest of the groups.
 340                  */
 341                 p = ((char *) bh->b_data) + (j >> 3);
 342                 r = memscan(p, 0, (EXT2_BLOCKS_PER_GROUP(sb) - j + 7) >> 3);
 343                 k = (r - ((char *) bh->b_data)) << 3;
 344                 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
 345                         j = k;
 346                         goto search_back;
 347                 }
 348                 k = find_next_zero_bit ((unsigned long *) bh->b_data, 
 349                                         EXT2_BLOCKS_PER_GROUP(sb),
 350                                         j);
 351                 if (k < EXT2_BLOCKS_PER_GROUP(sb)) {
 352                         j = k;
 353                         goto got_block;
 354                 }
 355         }
 356 
 357         ext2_debug ("Bit not found in block group %d.\n", i);
 358 
 359         /*
 360          * Now search the rest of the groups.  We assume that 
 361          * i and gdp correctly point to the last group visited.
 362          */
 363         for (k = 0; k < sb->u.ext2_sb.s_groups_count; k++) {
 364                 i++;
 365                 if (i >= sb->u.ext2_sb.s_groups_count)
 366                         i = 0;
 367                 gdp = get_group_desc (sb, i, &bh2);
 368                 if (gdp->bg_free_blocks_count > 0)
 369                         break;
 370         }
 371         if (k >= sb->u.ext2_sb.s_groups_count) {
 372                 unlock_super (sb);
 373                 return 0;
 374         }
 375         bitmap_nr = load_block_bitmap (sb, i);
 376         bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
 377         r = memscan(bh->b_data, 0, EXT2_BLOCKS_PER_GROUP(sb) >> 3);
 378         j = (r - bh->b_data) << 3;
 379         if (j < EXT2_BLOCKS_PER_GROUP(sb))
 380                 goto search_back;
 381         else
 382                 j = find_first_zero_bit ((unsigned long *) bh->b_data,
 383                                          EXT2_BLOCKS_PER_GROUP(sb));
 384         if (j >= EXT2_BLOCKS_PER_GROUP(sb)) {
 385                 ext2_error (sb, "ext2_new_block",
 386                             "Free blocks count corrupted for block group %d", i);
 387                 unlock_super (sb);
 388                 return 0;
 389         }
 390 
 391 search_back:
 392         /* 
 393          * We have succeeded in finding a free byte in the block
 394          * bitmap.  Now search backwards up to 7 bits to find the
 395          * start of this group of free blocks.
 396          */
 397         for (k = 0; k < 7 && j > 0 && !test_bit (j - 1, bh->b_data); k++, j--);
 398         
 399 got_block:
 400 
 401         ext2_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count);
 402 
 403         /*
 404          * Check quota for allocation of this block.
 405          */
 406         if (sb->dq_op)
 407                 if (sb->dq_op->alloc_block (inode, fs_to_dq_blocks(1, sb->s_blocksize))) {
 408                         unlock_super (sb);
 409                         *err = -EDQUOT;
 410                         return 0;
 411                 }
 412 
 413         tmp = j + i * EXT2_BLOCKS_PER_GROUP(sb) + es->s_first_data_block;
 414 
 415         if (test_opt (sb, CHECK_STRICT) &&
 416             (tmp == gdp->bg_block_bitmap ||
 417              tmp == gdp->bg_inode_bitmap ||
 418              in_range (tmp, gdp->bg_inode_table, sb->u.ext2_sb.s_itb_per_group)))
 419                 ext2_panic (sb, "ext2_new_block",
 420                             "Allocating block in system zone - "
 421                             "block = %u", tmp);
 422 
 423         if (set_bit (j, bh->b_data)) {
 424                 ext2_warning (sb, "ext2_new_block",
 425                               "bit already set for block %d", j);
 426                 if (sb->dq_op)
 427                         sb->dq_op->free_block(inode, fs_to_dq_blocks(1, sb->s_blocksize));
 428                 goto repeat;
 429         }
 430 
 431         ext2_debug ("found bit %d\n", j);
 432 
 433         /*
 434          * Do block preallocation now if required.
 435          */
 436 #ifdef EXT2_PREALLOCATE
 437         if (prealloc_block) {
 438                 *prealloc_count = 0;
 439                 *prealloc_block = tmp + 1;
 440                 for (k = 1;
 441                      k < 8 && (j + k) < EXT2_BLOCKS_PER_GROUP(sb); k++) {
 442                         if (sb->dq_op)
 443                                 if (sb->dq_op->alloc_block(inode, fs_to_dq_blocks(1, sb->s_blocksize)))
 444                                         break;
 445                         if (set_bit (j + k, bh->b_data)) {
 446                                 if (sb->dq_op)
 447                                         sb->dq_op->free_block(inode, fs_to_dq_blocks(1, sb->s_blocksize));
 448                                 break;
 449                         }
 450                         (*prealloc_count)++;
 451                 }       
 452                 gdp->bg_free_blocks_count -= *prealloc_count;
 453                 es->s_free_blocks_count -= *prealloc_count;
 454                 ext2_debug ("Preallocated a further %lu bits.\n",
 455                             *prealloc_count);
 456         }
 457 #endif
 458 
 459         j = tmp;
 460 
 461         mark_buffer_dirty(bh, 1);
 462         if (sb->s_flags & MS_SYNCHRONOUS) {
 463                 ll_rw_block (WRITE, 1, &bh);
 464                 wait_on_buffer (bh);
 465         }
 466 
 467         if (j >= es->s_blocks_count) {
 468                 ext2_error (sb, "ext2_new_block",
 469                             "block >= blocks count - "
 470                             "block_group = %d, block=%d", i, j);
 471                 unlock_super (sb);
 472                 return 0;
 473         }
 474         if (!(bh = getblk (sb->s_dev, j, sb->s_blocksize))) {
 475                 ext2_error (sb, "ext2_new_block", "cannot get block %d", j);
 476                 unlock_super (sb);
 477                 return 0;
 478         }
 479         memset(bh->b_data, 0, sb->s_blocksize);
 480         mark_buffer_uptodate(bh, 1);
 481         mark_buffer_dirty(bh, 1);
 482         brelse (bh);
 483 
 484         ext2_debug ("allocating block %d. "
 485                     "Goal hits %d of %d.\n", j, goal_hits, goal_attempts);
 486 
 487         gdp->bg_free_blocks_count--;
 488         mark_buffer_dirty(bh2, 1);
 489         es->s_free_blocks_count--;
 490         mark_buffer_dirty(sb->u.ext2_sb.s_sbh, 1);
 491         sb->s_dirt = 1;
 492         unlock_super (sb);
 493         *err = 0;
 494         return j;
 495 }
 496 
 497 unsigned long ext2_count_free_blocks (struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 498 {
 499 #ifdef EXT2FS_DEBUG
 500         struct ext2_super_block * es;
 501         unsigned long desc_count, bitmap_count, x;
 502         int bitmap_nr;
 503         struct ext2_group_desc * gdp;
 504         int i;
 505         
 506         lock_super (sb);
 507         es = sb->u.ext2_sb.s_es;
 508         desc_count = 0;
 509         bitmap_count = 0;
 510         gdp = NULL;
 511         for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
 512                 gdp = get_group_desc (sb, i, NULL);
 513                 desc_count += gdp->bg_free_blocks_count;
 514                 bitmap_nr = load_block_bitmap (sb, i);
 515                 x = ext2_count_free (sb->u.ext2_sb.s_block_bitmap[bitmap_nr],
 516                                      sb->s_blocksize);
 517                 printk ("group %d: stored = %d, counted = %lu\n",
 518                         i, gdp->bg_free_blocks_count, x);
 519                 bitmap_count += x;
 520         }
 521         printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
 522                es->s_free_blocks_count, desc_count, bitmap_count);
 523         unlock_super (sb);
 524         return bitmap_count;
 525 #else
 526         return sb->u.ext2_sb.s_es->s_free_blocks_count;
 527 #endif
 528 }
 529 
 530 static inline int block_in_use (unsigned long block,
     /* [previous][next][first][last][top][bottom][index][help] */
 531                                 struct super_block * sb,
 532                                 unsigned char * map)
 533 {
 534         return test_bit ((block - sb->u.ext2_sb.s_es->s_first_data_block) %
 535                          EXT2_BLOCKS_PER_GROUP(sb), map);
 536 }
 537 
 538 void ext2_check_blocks_bitmap (struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 539 {
 540         struct buffer_head * bh;
 541         struct ext2_super_block * es;
 542         unsigned long desc_count, bitmap_count, x;
 543         unsigned long desc_blocks;
 544         int bitmap_nr;
 545         struct ext2_group_desc * gdp;
 546         int i, j;
 547 
 548         lock_super (sb);
 549         es = sb->u.ext2_sb.s_es;
 550         desc_count = 0;
 551         bitmap_count = 0;
 552         gdp = NULL;
 553         desc_blocks = (sb->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
 554                       EXT2_DESC_PER_BLOCK(sb);
 555         for (i = 0; i < sb->u.ext2_sb.s_groups_count; i++) {
 556                 gdp = get_group_desc (sb, i, NULL);
 557                 desc_count += gdp->bg_free_blocks_count;
 558                 bitmap_nr = load_block_bitmap (sb, i);
 559                 bh = sb->u.ext2_sb.s_block_bitmap[bitmap_nr];
 560 
 561                 if (!test_bit (0, bh->b_data))
 562                         ext2_error (sb, "ext2_check_blocks_bitmap",
 563                                     "Superblock in group %d is marked free", i);
 564 
 565                 for (j = 0; j < desc_blocks; j++)
 566                         if (!test_bit (j + 1, bh->b_data))
 567                                 ext2_error (sb, "ext2_check_blocks_bitmap",
 568                                             "Descriptor block #%d in group "
 569                                             "%d is marked free", j, i);
 570 
 571                 if (!block_in_use (gdp->bg_block_bitmap, sb, bh->b_data))
 572                         ext2_error (sb, "ext2_check_blocks_bitmap",
 573                                     "Block bitmap for group %d is marked free",
 574                                     i);
 575 
 576                 if (!block_in_use (gdp->bg_inode_bitmap, sb, bh->b_data))
 577                         ext2_error (sb, "ext2_check_blocks_bitmap",
 578                                     "Inode bitmap for group %d is marked free",
 579                                     i);
 580 
 581                 for (j = 0; j < sb->u.ext2_sb.s_itb_per_group; j++)
 582                         if (!block_in_use (gdp->bg_inode_table + j, sb, bh->b_data))
 583                                 ext2_error (sb, "ext2_check_blocks_bitmap",
 584                                             "Block #%d of the inode table in "
 585                                             "group %d is marked free", j, i);
 586 
 587                 x = ext2_count_free (bh, sb->s_blocksize);
 588                 if (gdp->bg_free_blocks_count != x)
 589                         ext2_error (sb, "ext2_check_blocks_bitmap",
 590                                     "Wrong free blocks count for group %d, "
 591                                     "stored = %d, counted = %lu", i,
 592                                     gdp->bg_free_blocks_count, x);
 593                 bitmap_count += x;
 594         }
 595         if (es->s_free_blocks_count != bitmap_count)
 596                 ext2_error (sb, "ext2_check_blocks_bitmap",
 597                             "Wrong free blocks count in super block, "
 598                             "stored = %lu, counted = %lu",
 599                             (unsigned long) es->s_free_blocks_count, bitmap_count);
 600         unlock_super (sb);
 601 }

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