patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / ext2 / balloc.c
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  *  Big-endian to little-endian byte-swapping/bitmaps by
11  *        David S. Miller (davem@caip.rutgers.edu), 1995
12  */
13
14 #include <linux/config.h>
15 #include "ext2.h"
16 #include <linux/quotaops.h>
17 #include <linux/sched.h>
18 #include <linux/buffer_head.h>
19 #include <linux/vs_base.h>
20 #include <linux/vs_dlimit.h>
21
22 /*
23  * balloc.c contains the blocks allocation and deallocation routines
24  */
25
26 /*
27  * The free blocks are managed by bitmaps.  A file system contains several
28  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
29  * block for inodes, N blocks for the inode table and data blocks.
30  *
31  * The file system contains group descriptors which are located after the
32  * super block.  Each descriptor contains the number of the bitmap block and
33  * the free blocks count in the block.  The descriptors are loaded in memory
34  * when a file system is mounted (see ext2_read_super).
35  */
36
37
38 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
39
40 struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
41                                              unsigned int block_group,
42                                              struct buffer_head ** bh)
43 {
44         unsigned long group_desc;
45         unsigned long offset;
46         struct ext2_group_desc * desc;
47         struct ext2_sb_info *sbi = EXT2_SB(sb);
48
49         if (block_group >= sbi->s_groups_count) {
50                 ext2_error (sb, "ext2_get_group_desc",
51                             "block_group >= groups_count - "
52                             "block_group = %d, groups_count = %lu",
53                             block_group, sbi->s_groups_count);
54
55                 return NULL;
56         }
57         
58         group_desc = block_group / EXT2_DESC_PER_BLOCK(sb);
59         offset = block_group % EXT2_DESC_PER_BLOCK(sb);
60         if (!sbi->s_group_desc[group_desc]) {
61                 ext2_error (sb, "ext2_get_group_desc",
62                             "Group descriptor not loaded - "
63                             "block_group = %d, group_desc = %lu, desc = %lu",
64                              block_group, group_desc, offset);
65                 return NULL;
66         }
67         
68         desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data;
69         if (bh)
70                 *bh = sbi->s_group_desc[group_desc];
71         return desc + offset;
72 }
73
74 /*
75  * Read the bitmap for a given block_group, reading into the specified 
76  * slot in the superblock's bitmap cache.
77  *
78  * Return buffer_head on success or NULL in case of failure.
79  */
80 static struct buffer_head *
81 read_block_bitmap(struct super_block *sb, unsigned int block_group)
82 {
83         struct ext2_group_desc * desc;
84         struct buffer_head * bh = NULL;
85         
86         desc = ext2_get_group_desc (sb, block_group, NULL);
87         if (!desc)
88                 goto error_out;
89         bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap));
90         if (!bh)
91                 ext2_error (sb, "read_block_bitmap",
92                             "Cannot read block bitmap - "
93                             "block_group = %d, block_bitmap = %lu",
94                             block_group, (unsigned long) desc->bg_block_bitmap);
95 error_out:
96         return bh;
97 }
98
99 /*
100  * Set sb->s_dirt here because the superblock was "logically" altered.  We
101  * need to recalculate its free blocks count and flush it out.
102  */
103 static int reserve_blocks(struct super_block *sb, int count)
104 {
105         struct ext2_sb_info *sbi = EXT2_SB(sb);
106         struct ext2_super_block *es = sbi->s_es;
107         unsigned free_blocks;
108         unsigned root_blocks;
109
110         free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
111         root_blocks = le32_to_cpu(es->s_r_blocks_count);
112
113         DLIMIT_ADJUST_BLOCK(sb, vx_current_xid(), &free_blocks, &root_blocks);
114
115         if (free_blocks < count)
116                 count = free_blocks;
117
118         if (free_blocks < root_blocks + count && !capable(CAP_SYS_RESOURCE) &&
119             sbi->s_resuid != current->fsuid &&
120             (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
121                 /*
122                  * We are too close to reserve and we are not privileged.
123                  * Can we allocate anything at all?
124                  */
125                 if (free_blocks > root_blocks)
126                         count = free_blocks - root_blocks;
127                 else
128                         return 0;
129         }
130
131         percpu_counter_mod(&sbi->s_freeblocks_counter, -count);
132         sb->s_dirt = 1;
133         return count;
134 }
135
136 static void release_blocks(struct super_block *sb, int count)
137 {
138         if (count) {
139                 struct ext2_sb_info *sbi = EXT2_SB(sb);
140
141                 percpu_counter_mod(&sbi->s_freeblocks_counter, count);
142                 sb->s_dirt = 1;
143         }
144 }
145
146 static int group_reserve_blocks(struct ext2_sb_info *sbi, int group_no,
147         struct ext2_group_desc *desc, struct buffer_head *bh, int count)
148 {
149         unsigned free_blocks;
150
151         if (!desc->bg_free_blocks_count)
152                 return 0;
153
154         spin_lock(sb_bgl_lock(sbi, group_no));
155         free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
156         if (free_blocks < count)
157                 count = free_blocks;
158         desc->bg_free_blocks_count = cpu_to_le16(free_blocks - count);
159         spin_unlock(sb_bgl_lock(sbi, group_no));
160         mark_buffer_dirty(bh);
161         return count;
162 }
163
164 static void group_release_blocks(struct super_block *sb, int group_no,
165         struct ext2_group_desc *desc, struct buffer_head *bh, int count)
166 {
167         if (count) {
168                 struct ext2_sb_info *sbi = EXT2_SB(sb);
169                 unsigned free_blocks;
170
171                 spin_lock(sb_bgl_lock(sbi, group_no));
172                 free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
173                 desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count);
174                 spin_unlock(sb_bgl_lock(sbi, group_no));
175                 sb->s_dirt = 1;
176                 mark_buffer_dirty(bh);
177         }
178 }
179
180 /* Free given blocks, update quota and i_blocks field */
181 void ext2_free_blocks (struct inode * inode, unsigned long block,
182                        unsigned long count)
183 {
184         struct buffer_head *bitmap_bh = NULL;
185         struct buffer_head * bh2;
186         unsigned long block_group;
187         unsigned long bit;
188         unsigned long i;
189         unsigned long overflow;
190         struct super_block * sb = inode->i_sb;
191         struct ext2_sb_info * sbi = EXT2_SB(sb);
192         struct ext2_group_desc * desc;
193         struct ext2_super_block * es = sbi->s_es;
194         unsigned freed = 0, group_freed;
195
196         if (block < le32_to_cpu(es->s_first_data_block) ||
197             block + count < block ||
198             block + count > le32_to_cpu(es->s_blocks_count)) {
199                 ext2_error (sb, "ext2_free_blocks",
200                             "Freeing blocks not in datazone - "
201                             "block = %lu, count = %lu", block, count);
202                 goto error_return;
203         }
204
205         ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
206
207 do_more:
208         overflow = 0;
209         block_group = (block - le32_to_cpu(es->s_first_data_block)) /
210                       EXT2_BLOCKS_PER_GROUP(sb);
211         bit = (block - le32_to_cpu(es->s_first_data_block)) %
212                       EXT2_BLOCKS_PER_GROUP(sb);
213         /*
214          * Check to see if we are freeing blocks across a group
215          * boundary.
216          */
217         if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {
218                 overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb);
219                 count -= overflow;
220         }
221         brelse(bitmap_bh);
222         bitmap_bh = read_block_bitmap(sb, block_group);
223         if (!bitmap_bh)
224                 goto error_return;
225
226         desc = ext2_get_group_desc (sb, block_group, &bh2);
227         if (!desc)
228                 goto error_return;
229
230         if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
231             in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
232             in_range (block, le32_to_cpu(desc->bg_inode_table),
233                       sbi->s_itb_per_group) ||
234             in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
235                       sbi->s_itb_per_group))
236                 ext2_error (sb, "ext2_free_blocks",
237                             "Freeing blocks in system zones - "
238                             "Block = %lu, count = %lu",
239                             block, count);
240
241         for (i = 0, group_freed = 0; i < count; i++) {
242                 if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
243                                         bit + i, (void *) bitmap_bh->b_data))
244                         ext2_error (sb, "ext2_free_blocks",
245                                       "bit already cleared for block %lu",
246                                       block + i);
247                 else
248                         group_freed++;
249         }
250
251         mark_buffer_dirty(bitmap_bh);
252         if (sb->s_flags & MS_SYNCHRONOUS)
253                 sync_dirty_buffer(bitmap_bh);
254
255         group_release_blocks(sb, block_group, desc, bh2, group_freed);
256         freed += group_freed;
257
258         if (overflow) {
259                 block += count;
260                 count = overflow;
261                 goto do_more;
262         }
263 error_return:
264         brelse(bitmap_bh);
265         DLIMIT_FREE_BLOCK(sb, inode->i_xid, freed);
266         release_blocks(sb, freed);
267         DQUOT_FREE_BLOCK(inode, freed);
268 }
269
270 static int grab_block(spinlock_t *lock, char *map, unsigned size, int goal)
271 {
272         int k;
273         char *p, *r;
274
275         if (!ext2_test_bit(goal, map))
276                 goto got_it;
277
278 repeat:
279         if (goal) {
280                 /*
281                  * The goal was occupied; search forward for a free 
282                  * block within the next XX blocks.
283                  *
284                  * end_goal is more or less random, but it has to be
285                  * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
286                  * next 64-bit boundary is simple..
287                  */
288                 k = (goal + 63) & ~63;
289                 goal = ext2_find_next_zero_bit(map, k, goal);
290                 if (goal < k)
291                         goto got_it;
292                 /*
293                  * Search in the remainder of the current group.
294                  */
295         }
296
297         p = map + (goal >> 3);
298         r = memscan(p, 0, (size - goal + 7) >> 3);
299         k = (r - map) << 3;
300         if (k < size) {
301                 /* 
302                  * We have succeeded in finding a free byte in the block
303                  * bitmap.  Now search backwards to find the start of this
304                  * group of free blocks - won't take more than 7 iterations.
305                  */
306                 for (goal = k; goal && !ext2_test_bit (goal - 1, map); goal--)
307                         ;
308                 goto got_it;
309         }
310
311         k = ext2_find_next_zero_bit ((u32 *)map, size, goal);
312         if (k < size) {
313                 goal = k;
314                 goto got_it;
315         }
316         return -1;
317 got_it:
318         if (ext2_set_bit_atomic(lock, goal, (void *) map)) 
319                 goto repeat;    
320         return goal;
321 }
322
323 /*
324  * ext2_new_block uses a goal block to assist allocation.  If the goal is
325  * free, or there is a free block within 32 blocks of the goal, that block
326  * is allocated.  Otherwise a forward search is made for a free block; within 
327  * each block group the search first looks for an entire free byte in the block
328  * bitmap, and then for any free bit if that fails.
329  * This function also updates quota and i_blocks field.
330  */
331 int ext2_new_block(struct inode *inode, unsigned long goal,
332                         u32 *prealloc_count, u32 *prealloc_block, int *err)
333 {
334         struct buffer_head *bitmap_bh = NULL;
335         struct buffer_head *gdp_bh;     /* bh2 */
336         struct ext2_group_desc *desc;
337         int group_no;                   /* i */
338         int ret_block;                  /* j */
339         int group_idx;                  /* k */
340         int target_block;               /* tmp */
341         int block = 0;
342         struct super_block *sb = inode->i_sb;
343         struct ext2_sb_info *sbi = EXT2_SB(sb);
344         struct ext2_super_block *es = sbi->s_es;
345         unsigned group_size = EXT2_BLOCKS_PER_GROUP(sb);
346         unsigned prealloc_goal = es->s_prealloc_blocks;
347         unsigned group_alloc = 0, es_alloc, dq_alloc;
348         int nr_scanned_groups;
349
350         if (!prealloc_goal--)
351                 prealloc_goal = EXT2_DEFAULT_PREALLOC_BLOCKS - 1;
352         if (!prealloc_count || *prealloc_count)
353                 prealloc_goal = 0;
354
355         if (DQUOT_ALLOC_BLOCK(inode, 1)) {
356                 *err = -EDQUOT;
357                 goto out;
358         }
359
360         while (prealloc_goal && DQUOT_PREALLOC_BLOCK(inode, prealloc_goal))
361                 prealloc_goal--;
362
363         dq_alloc = prealloc_goal + 1;
364         es_alloc = reserve_blocks(sb, dq_alloc);
365         if (!es_alloc) {
366                 *err = -ENOSPC;
367                 goto out_dquot;
368         }
369         if (DLIMIT_ALLOC_BLOCK(sb, inode->i_xid, es_alloc)) {
370                 *err = -ENOSPC;
371                 goto out_dlimit;
372         }
373
374         ext2_debug ("goal=%lu.\n", goal);
375
376         if (goal < le32_to_cpu(es->s_first_data_block) ||
377             goal >= le32_to_cpu(es->s_blocks_count))
378                 goal = le32_to_cpu(es->s_first_data_block);
379         group_no = (goal - le32_to_cpu(es->s_first_data_block)) / group_size;
380         desc = ext2_get_group_desc (sb, group_no, &gdp_bh);
381         if (!desc) {
382                 /*
383                  * gdp_bh may still be uninitialised.  But group_release_blocks
384                  * will not touch it because group_alloc is zero.
385                  */
386                 goto io_error;
387         }
388
389         group_alloc = group_reserve_blocks(sbi, group_no, desc,
390                                         gdp_bh, es_alloc);
391         if (group_alloc) {
392                 ret_block = ((goal - le32_to_cpu(es->s_first_data_block)) %
393                                         group_size);
394                 brelse(bitmap_bh);
395                 bitmap_bh = read_block_bitmap(sb, group_no);
396                 if (!bitmap_bh)
397                         goto io_error;
398                 
399                 ext2_debug("goal is at %d:%d.\n", group_no, ret_block);
400
401                 ret_block = grab_block(sb_bgl_lock(sbi, group_no),
402                                 bitmap_bh->b_data, group_size, ret_block);
403                 if (ret_block >= 0)
404                         goto got_block;
405                 group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
406                 group_alloc = 0;
407         }
408
409         ext2_debug ("Bit not found in block group %d.\n", group_no);
410
411         /*
412          * Now search the rest of the groups.  We assume that 
413          * i and desc correctly point to the last group visited.
414          */
415         nr_scanned_groups = 0;
416 retry:
417         for (group_idx = 0; !group_alloc &&
418                         group_idx < sbi->s_groups_count; group_idx++) {
419                 group_no++;
420                 if (group_no >= sbi->s_groups_count)
421                         group_no = 0;
422                 desc = ext2_get_group_desc(sb, group_no, &gdp_bh);
423                 if (!desc)
424                         goto io_error;
425                 group_alloc = group_reserve_blocks(sbi, group_no, desc,
426                                                 gdp_bh, es_alloc);
427         }
428         if (!group_alloc) {
429                 *err = -ENOSPC;
430                 goto out_release;
431         }
432         brelse(bitmap_bh);
433         bitmap_bh = read_block_bitmap(sb, group_no);
434         if (!bitmap_bh)
435                 goto io_error;
436
437         ret_block = grab_block(sb_bgl_lock(sbi, group_no), bitmap_bh->b_data,
438                                 group_size, 0);
439         if (ret_block < 0) {
440                 /*
441                  * If a free block counter is corrupted we can loop inifintely.
442                  * Detect that here.
443                  */
444                 nr_scanned_groups++;
445                 if (nr_scanned_groups > 2 * sbi->s_groups_count) {
446                         ext2_error(sb, "ext2_new_block",
447                                 "corrupted free blocks counters");
448                         goto io_error;
449                 }
450                 /*
451                  * Someone else grabbed the last free block in this blockgroup
452                  * before us.  Retry the scan.
453                  */
454                 group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
455                 group_alloc = 0;
456                 goto retry;
457         }
458
459 got_block:
460         ext2_debug("using block group %d(%d)\n",
461                 group_no, desc->bg_free_blocks_count);
462
463         target_block = ret_block + group_no * group_size +
464                         le32_to_cpu(es->s_first_data_block);
465
466         if (target_block == le32_to_cpu(desc->bg_block_bitmap) ||
467             target_block == le32_to_cpu(desc->bg_inode_bitmap) ||
468             in_range(target_block, le32_to_cpu(desc->bg_inode_table),
469                       sbi->s_itb_per_group))
470                 ext2_error (sb, "ext2_new_block",
471                             "Allocating block in system zone - "
472                             "block = %u", target_block);
473
474         if (target_block >= le32_to_cpu(es->s_blocks_count)) {
475                 ext2_error (sb, "ext2_new_block",
476                             "block(%d) >= blocks count(%d) - "
477                             "block_group = %d, es == %p ", ret_block,
478                         le32_to_cpu(es->s_blocks_count), group_no, es);
479                 goto io_error;
480         }
481         block = target_block;
482
483         /* OK, we _had_ allocated something */
484         ext2_debug("found bit %d\n", ret_block);
485
486         dq_alloc--;
487         es_alloc--;
488         group_alloc--;
489
490         /*
491          * Do block preallocation now if required.
492          */
493         write_lock(&EXT2_I(inode)->i_meta_lock);
494         if (group_alloc && !*prealloc_count) {
495                 unsigned n;
496
497                 for (n = 0; n < group_alloc && ++ret_block < group_size; n++) {
498                         if (ext2_set_bit_atomic(sb_bgl_lock(sbi, group_no),
499                                                 ret_block,
500                                                 (void*) bitmap_bh->b_data))
501                                 break;
502                 }
503                 *prealloc_block = block + 1;
504                 *prealloc_count = n;
505                 es_alloc -= n;
506                 dq_alloc -= n;
507                 group_alloc -= n;
508         }
509         write_unlock(&EXT2_I(inode)->i_meta_lock);
510
511         mark_buffer_dirty(bitmap_bh);
512         if (sb->s_flags & MS_SYNCHRONOUS)
513                 sync_dirty_buffer(bitmap_bh);
514
515         ext2_debug ("allocating block %d. ", block);
516
517         *err = 0;
518 out_release:
519         group_release_blocks(sb, group_no, desc, gdp_bh, group_alloc);
520         DLIMIT_FREE_BLOCK(sb, inode->i_xid, es_alloc);
521 out_dlimit:
522         release_blocks(sb, es_alloc);
523 out_dquot:
524         DQUOT_FREE_BLOCK(inode, dq_alloc);
525 out:
526         brelse(bitmap_bh);
527         return block;
528
529 io_error:
530         *err = -EIO;
531         goto out_release;
532 }
533
534 unsigned long ext2_count_free_blocks (struct super_block * sb)
535 {
536         struct ext2_group_desc * desc;
537         unsigned long desc_count = 0;
538         int i;
539 #ifdef EXT2FS_DEBUG
540         unsigned long bitmap_count, x;
541         struct ext2_super_block *es;
542
543         lock_super (sb);
544         es = EXT2_SB(sb)->s_es;
545         desc_count = 0;
546         bitmap_count = 0;
547         desc = NULL;
548         for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
549                 struct buffer_head *bitmap_bh;
550                 desc = ext2_get_group_desc (sb, i, NULL);
551                 if (!desc)
552                         continue;
553                 desc_count += le16_to_cpu(desc->bg_free_blocks_count);
554                 bitmap_bh = read_block_bitmap(sb, i);
555                 if (!bitmap_bh)
556                         continue;
557                 
558                 x = ext2_count_free(bitmap_bh, sb->s_blocksize);
559                 printk ("group %d: stored = %d, counted = %lu\n",
560                         i, le16_to_cpu(desc->bg_free_blocks_count), x);
561                 bitmap_count += x;
562                 brelse(bitmap_bh);
563         }
564         printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
565                 (long)le32_to_cpu(es->s_free_blocks_count),
566                 desc_count, bitmap_count);
567         unlock_super (sb);
568         return bitmap_count;
569 #else
570         for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
571                 desc = ext2_get_group_desc (sb, i, NULL);
572                 if (!desc)
573                         continue;
574                 desc_count += le16_to_cpu(desc->bg_free_blocks_count);
575         }
576         return desc_count;
577 #endif
578 }
579
580 static inline int
581 block_in_use(unsigned long block, struct super_block *sb, unsigned char *map)
582 {
583         return ext2_test_bit ((block - le32_to_cpu(EXT2_SB(sb)->s_es->s_first_data_block)) %
584                          EXT2_BLOCKS_PER_GROUP(sb), map);
585 }
586
587 static inline int test_root(int a, int b)
588 {
589         if (a == 0)
590                 return 1;
591         while (1) {
592                 if (a == 1)
593                         return 1;
594                 if (a % b)
595                         return 0;
596                 a = a / b;
597         }
598 }
599
600 static int ext2_group_sparse(int group)
601 {
602         return (test_root(group, 3) || test_root(group, 5) ||
603                 test_root(group, 7));
604 }
605
606 /**
607  *      ext2_bg_has_super - number of blocks used by the superblock in group
608  *      @sb: superblock for filesystem
609  *      @group: group number to check
610  *
611  *      Return the number of blocks used by the superblock (primary or backup)
612  *      in this group.  Currently this will be only 0 or 1.
613  */
614 int ext2_bg_has_super(struct super_block *sb, int group)
615 {
616         if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
617             !ext2_group_sparse(group))
618                 return 0;
619         return 1;
620 }
621
622 /**
623  *      ext2_bg_num_gdb - number of blocks used by the group table in group
624  *      @sb: superblock for filesystem
625  *      @group: group number to check
626  *
627  *      Return the number of blocks used by the group descriptor table
628  *      (primary or backup) in this group.  In the future there may be a
629  *      different number of descriptor blocks in each group.
630  */
631 unsigned long ext2_bg_num_gdb(struct super_block *sb, int group)
632 {
633         if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
634             !ext2_group_sparse(group))
635                 return 0;
636         return EXT2_SB(sb)->s_gdb_count;
637 }
638
639 #ifdef CONFIG_EXT2_CHECK
640 /* Called at mount-time, super-block is locked */
641 void ext2_check_blocks_bitmap (struct super_block * sb)
642 {
643         struct buffer_head *bitmap_bh = NULL;
644         struct ext2_super_block * es;
645         unsigned long desc_count, bitmap_count, x, j;
646         unsigned long desc_blocks;
647         struct ext2_group_desc * desc;
648         int i;
649
650         es = EXT2_SB(sb)->s_es;
651         desc_count = 0;
652         bitmap_count = 0;
653         desc = NULL;
654         for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
655                 desc = ext2_get_group_desc (sb, i, NULL);
656                 if (!desc)
657                         continue;
658                 desc_count += le16_to_cpu(desc->bg_free_blocks_count);
659                 brelse(bitmap_bh);
660                 bitmap_bh = read_block_bitmap(sb, i);
661                 if (!bitmap_bh)
662                         continue;
663
664                 if (ext2_bg_has_super(sb, i) &&
665                                 !ext2_test_bit(0, bitmap_bh->b_data))
666                         ext2_error(sb, __FUNCTION__,
667                                    "Superblock in group %d is marked free", i);
668
669                 desc_blocks = ext2_bg_num_gdb(sb, i);
670                 for (j = 0; j < desc_blocks; j++)
671                         if (!ext2_test_bit(j + 1, bitmap_bh->b_data))
672                                 ext2_error(sb, __FUNCTION__,
673                                            "Descriptor block #%ld in group "
674                                            "%d is marked free", j, i);
675
676                 if (!block_in_use(le32_to_cpu(desc->bg_block_bitmap),
677                                         sb, bitmap_bh->b_data))
678                         ext2_error(sb, "ext2_check_blocks_bitmap",
679                                     "Block bitmap for group %d is marked free",
680                                     i);
681
682                 if (!block_in_use(le32_to_cpu(desc->bg_inode_bitmap),
683                                         sb, bitmap_bh->b_data))
684                         ext2_error(sb, "ext2_check_blocks_bitmap",
685                                     "Inode bitmap for group %d is marked free",
686                                     i);
687
688                 for (j = 0; j < EXT2_SB(sb)->s_itb_per_group; j++)
689                         if (!block_in_use(le32_to_cpu(desc->bg_inode_table) + j,
690                                                 sb, bitmap_bh->b_data))
691                                 ext2_error (sb, "ext2_check_blocks_bitmap",
692                                             "Block #%ld of the inode table in "
693                                             "group %d is marked free", j, i);
694
695                 x = ext2_count_free(bitmap_bh, sb->s_blocksize);
696                 if (le16_to_cpu(desc->bg_free_blocks_count) != x)
697                         ext2_error (sb, "ext2_check_blocks_bitmap",
698                                     "Wrong free blocks count for group %d, "
699                                     "stored = %d, counted = %lu", i,
700                                     le16_to_cpu(desc->bg_free_blocks_count), x);
701                 bitmap_count += x;
702         }
703         if (le32_to_cpu(es->s_free_blocks_count) != bitmap_count)
704                 ext2_error (sb, "ext2_check_blocks_bitmap",
705                         "Wrong free blocks count in super block, "
706                         "stored = %lu, counted = %lu",
707                         (unsigned long)le32_to_cpu(es->s_free_blocks_count),
708                         bitmap_count);
709         brelse(bitmap_bh);
710 }
711 #endif