2 * linux/fs/ext3/balloc.c
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)
9 * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
10 * Big-endian to little-endian byte-swapping/bitmaps by
11 * David S. Miller (davem@caip.rutgers.edu), 1995
14 #include <linux/config.h>
15 #include <linux/time.h>
17 #include <linux/jbd.h>
18 #include <linux/ext3_fs.h>
19 #include <linux/ext3_jbd.h>
20 #include <linux/quotaops.h>
21 #include <linux/buffer_head.h>
22 #include <linux/vs_dlimit.h>
25 * balloc.c contains the blocks allocation and deallocation routines
29 * The free blocks are managed by bitmaps. A file system contains several
30 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
31 * block for inodes, N blocks for the inode table and data blocks.
33 * The file system contains group descriptors which are located after the
34 * super block. Each descriptor contains the number of the bitmap block and
35 * the free blocks count in the block. The descriptors are loaded in memory
36 * when a file system is mounted (see ext3_read_super).
40 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
42 struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
43 unsigned int block_group,
44 struct buffer_head ** bh)
46 unsigned long group_desc;
48 struct ext3_group_desc * gdp;
50 if (block_group >= EXT3_SB(sb)->s_groups_count) {
51 ext3_error (sb, "ext3_get_group_desc",
52 "block_group >= groups_count - "
53 "block_group = %d, groups_count = %lu",
54 block_group, EXT3_SB(sb)->s_groups_count);
59 group_desc = block_group / EXT3_DESC_PER_BLOCK(sb);
60 desc = block_group % EXT3_DESC_PER_BLOCK(sb);
61 if (!EXT3_SB(sb)->s_group_desc[group_desc]) {
62 ext3_error (sb, "ext3_get_group_desc",
63 "Group descriptor not loaded - "
64 "block_group = %d, group_desc = %lu, desc = %lu",
65 block_group, group_desc, desc);
69 gdp = (struct ext3_group_desc *)
70 EXT3_SB(sb)->s_group_desc[group_desc]->b_data;
72 *bh = EXT3_SB(sb)->s_group_desc[group_desc];
77 * Read the bitmap for a given block_group, reading into the specified
78 * slot in the superblock's bitmap cache.
80 * Return buffer_head on success or NULL in case of failure.
82 static struct buffer_head *
83 read_block_bitmap(struct super_block *sb, unsigned int block_group)
85 struct ext3_group_desc * desc;
86 struct buffer_head * bh = NULL;
88 desc = ext3_get_group_desc (sb, block_group, NULL);
91 bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap));
93 ext3_error (sb, "read_block_bitmap",
94 "Cannot read block bitmap - "
95 "block_group = %d, block_bitmap = %lu",
96 block_group, (unsigned long) desc->bg_block_bitmap);
101 /* Free given blocks, update quota and i_blocks field */
102 void ext3_free_blocks (handle_t *handle, struct inode * inode,
103 unsigned long block, unsigned long count)
105 struct buffer_head *bitmap_bh = NULL;
106 struct buffer_head *gd_bh;
107 unsigned long block_group;
110 unsigned long overflow;
111 struct super_block * sb;
112 struct ext3_group_desc * gdp;
113 struct ext3_super_block * es;
114 struct ext3_sb_info *sbi;
116 int dquot_freed_blocks = 0;
120 printk ("ext3_free_blocks: nonexistent device");
124 es = EXT3_SB(sb)->s_es;
125 if (block < le32_to_cpu(es->s_first_data_block) ||
126 block + count < block ||
127 block + count > le32_to_cpu(es->s_blocks_count)) {
128 ext3_error (sb, "ext3_free_blocks",
129 "Freeing blocks not in datazone - "
130 "block = %lu, count = %lu", block, count);
134 ext3_debug ("freeing block %lu\n", block);
138 block_group = (block - le32_to_cpu(es->s_first_data_block)) /
139 EXT3_BLOCKS_PER_GROUP(sb);
140 bit = (block - le32_to_cpu(es->s_first_data_block)) %
141 EXT3_BLOCKS_PER_GROUP(sb);
143 * Check to see if we are freeing blocks across a group
146 if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
147 overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
151 bitmap_bh = read_block_bitmap(sb, block_group);
154 gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
158 if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
159 in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
160 in_range (block, le32_to_cpu(gdp->bg_inode_table),
161 EXT3_SB(sb)->s_itb_per_group) ||
162 in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
163 EXT3_SB(sb)->s_itb_per_group))
164 ext3_error (sb, "ext3_free_blocks",
165 "Freeing blocks in system zones - "
166 "Block = %lu, count = %lu",
170 * We are about to start releasing blocks in the bitmap,
171 * so we need undo access.
173 /* @@@ check errors */
174 BUFFER_TRACE(bitmap_bh, "getting undo access");
175 err = ext3_journal_get_undo_access(handle, bitmap_bh, NULL);
180 * We are about to modify some metadata. Call the journal APIs
181 * to unshare ->b_data if a currently-committing transaction is
184 BUFFER_TRACE(gd_bh, "get_write_access");
185 err = ext3_journal_get_write_access(handle, gd_bh);
189 jbd_lock_bh_state(bitmap_bh);
191 for (i = 0; i < count; i++) {
193 * An HJ special. This is expensive...
195 #ifdef CONFIG_JBD_DEBUG
196 jbd_unlock_bh_state(bitmap_bh);
198 struct buffer_head *debug_bh;
199 debug_bh = sb_find_get_block(sb, block + i);
201 BUFFER_TRACE(debug_bh, "Deleted!");
202 if (!bh2jh(bitmap_bh)->b_committed_data)
203 BUFFER_TRACE(debug_bh,
204 "No commited data in bitmap");
205 BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
209 jbd_lock_bh_state(bitmap_bh);
211 /* @@@ This prevents newly-allocated data from being
212 * freed and then reallocated within the same
215 * Ideally we would want to allow that to happen, but to
216 * do so requires making journal_forget() capable of
217 * revoking the queued write of a data block, which
218 * implies blocking on the journal lock. *forget()
219 * cannot block due to truncate races.
221 * Eventually we can fix this by making journal_forget()
222 * return a status indicating whether or not it was able
223 * to revoke the buffer. On successful revoke, it is
224 * safe not to set the allocation bit in the committed
225 * bitmap, because we know that there is no outstanding
226 * activity on the buffer any more and so it is safe to
229 BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
230 J_ASSERT_BH(bitmap_bh,
231 bh2jh(bitmap_bh)->b_committed_data != NULL);
232 ext3_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i,
233 bh2jh(bitmap_bh)->b_committed_data);
236 * We clear the bit in the bitmap after setting the committed
237 * data bit, because this is the reverse order to that which
238 * the allocator uses.
240 BUFFER_TRACE(bitmap_bh, "clear bit");
241 if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
242 bit + i, bitmap_bh->b_data)) {
243 jbd_unlock_bh_state(bitmap_bh);
244 ext3_error(sb, __FUNCTION__,
245 "bit already cleared for block %lu", block + i);
246 jbd_lock_bh_state(bitmap_bh);
247 BUFFER_TRACE(bitmap_bh, "bit already cleared");
249 dquot_freed_blocks++;
252 jbd_unlock_bh_state(bitmap_bh);
254 spin_lock(sb_bgl_lock(sbi, block_group));
255 gdp->bg_free_blocks_count =
256 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) +
258 spin_unlock(sb_bgl_lock(sbi, block_group));
259 percpu_counter_mod(&sbi->s_freeblocks_counter, count);
261 /* We dirtied the bitmap block */
262 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
263 err = ext3_journal_dirty_metadata(handle, bitmap_bh);
265 /* And the group descriptor block */
266 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
267 ret = ext3_journal_dirty_metadata(handle, gd_bh);
270 if (overflow && !err) {
278 ext3_std_error(sb, err);
279 if (dquot_freed_blocks) {
280 DLIMIT_FREE_BLOCK(sb, inode->i_xid, dquot_freed_blocks);
281 DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
287 * For ext3 allocations, we must not reuse any blocks which are
288 * allocated in the bitmap buffer's "last committed data" copy. This
289 * prevents deletes from freeing up the page for reuse until we have
290 * committed the delete transaction.
292 * If we didn't do this, then deleting something and reallocating it as
293 * data would allow the old block to be overwritten before the
294 * transaction committed (because we force data to disk before commit).
295 * This would lead to corruption if we crashed between overwriting the
296 * data and committing the delete.
298 * @@@ We may want to make this allocation behaviour conditional on
299 * data-writes at some point, and disable it for metadata allocations or
302 static inline int ext3_test_allocatable(int nr, struct buffer_head *bh)
305 struct journal_head *jh = bh2jh(bh);
307 if (ext3_test_bit(nr, bh->b_data))
310 jbd_lock_bh_state(bh);
311 if (!jh->b_committed_data)
314 ret = !ext3_test_bit(nr, jh->b_committed_data);
315 jbd_unlock_bh_state(bh);
320 * Find an allocatable block in a bitmap. We honour both the bitmap and
321 * its last-committed copy (if that exists), and perform the "most
322 * appropriate allocation" algorithm of looking for a free block near
323 * the initial goal; then for a free byte somewhere in the bitmap; then
324 * for any free bit in the bitmap.
327 find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)
331 struct journal_head *jh = bh2jh(bh);
335 * The goal was occupied; search forward for a free
336 * block within the next XX blocks.
338 * end_goal is more or less random, but it has to be
339 * less than EXT3_BLOCKS_PER_GROUP. Aligning up to the
340 * next 64-bit boundary is simple..
342 int end_goal = (start + 63) & ~63;
343 here = ext3_find_next_zero_bit(bh->b_data, end_goal, start);
344 if (here < end_goal && ext3_test_allocatable(here, bh))
346 ext3_debug("Bit not found near goal\n");
353 p = ((char *)bh->b_data) + (here >> 3);
354 r = memscan(p, 0, (maxblocks - here + 7) >> 3);
355 next = (r - ((char *)bh->b_data)) << 3;
357 if (next < maxblocks && ext3_test_allocatable(next, bh))
361 * The bitmap search --- search forward alternately through the actual
362 * bitmap and the last-committed copy until we find a bit free in
365 while (here < maxblocks) {
366 next = ext3_find_next_zero_bit(bh->b_data, maxblocks, here);
367 if (next >= maxblocks)
369 if (ext3_test_allocatable(next, bh))
371 jbd_lock_bh_state(bh);
372 if (jh->b_committed_data)
373 here = ext3_find_next_zero_bit(jh->b_committed_data,
375 jbd_unlock_bh_state(bh);
381 * We think we can allocate this block in this bitmap. Try to set the bit.
382 * If that succeeds then check that nobody has allocated and then freed the
383 * block since we saw that is was not marked in b_committed_data. If it _was_
384 * allocated and freed then clear the bit in the bitmap again and return
388 claim_block(spinlock_t *lock, int block, struct buffer_head *bh)
390 struct journal_head *jh = bh2jh(bh);
393 if (ext3_set_bit_atomic(lock, block, bh->b_data))
395 jbd_lock_bh_state(bh);
396 if (jh->b_committed_data && ext3_test_bit(block,jh->b_committed_data)) {
397 ext3_clear_bit_atomic(lock, block, bh->b_data);
402 jbd_unlock_bh_state(bh);
407 * If we failed to allocate the desired block then we may end up crossing to a
408 * new bitmap. In that case we must release write access to the old one via
409 * ext3_journal_release_buffer(), else we'll run out of credits.
412 ext3_try_to_allocate(struct super_block *sb, handle_t *handle, int group,
413 struct buffer_head *bitmap_bh, int goal, int *errp)
422 * Make sure we use undo access for the bitmap, because it is critical
423 * that we do the frozen_data COW on bitmap buffers in all cases even
424 * if the buffer is in BJ_Forget state in the committing transaction.
426 BUFFER_TRACE(bitmap_bh, "get undo access for new block");
427 fatal = ext3_journal_get_undo_access(handle, bitmap_bh, &credits);
434 if (goal < 0 || !ext3_test_allocatable(goal, bitmap_bh)) {
435 goal = find_next_usable_block(goal, bitmap_bh,
436 EXT3_BLOCKS_PER_GROUP(sb));
440 for (i = 0; i < 7 && goal > 0 &&
441 ext3_test_allocatable(goal - 1, bitmap_bh);
445 if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group), goal, bitmap_bh)) {
447 * The block was allocated by another thread, or it was
448 * allocated and then freed by another thread
451 if (goal >= EXT3_BLOCKS_PER_GROUP(sb))
456 BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for bitmap block");
457 fatal = ext3_journal_dirty_metadata(handle, bitmap_bh);
465 BUFFER_TRACE(bitmap_bh, "journal_release_buffer");
466 ext3_journal_release_buffer(handle, bitmap_bh, credits);
471 static int ext3_has_free_blocks(struct super_block *sb)
473 struct ext3_sb_info *sbi = EXT3_SB(sb);
474 int free_blocks, root_blocks;
476 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
477 root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
479 DLIMIT_ADJUST_BLOCK(sb, vx_current_xid(), &free_blocks, &root_blocks);
481 if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
482 sbi->s_resuid != current->fsuid &&
483 (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
490 * ext3_should_retry_alloc() is called when ENOSPC is returned, and if
491 * it is profitable to retry the operation, this function will wait
492 * for the current or commiting transaction to complete, and then
495 int ext3_should_retry_alloc(struct super_block *sb, int *retries)
497 if (!ext3_has_free_blocks(sb) || (*retries)++ > 3)
500 jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
502 return journal_force_commit_nested(EXT3_SB(sb)->s_journal);
506 * ext3_new_block uses a goal block to assist allocation. If the goal is
507 * free, or there is a free block within 32 blocks of the goal, that block
508 * is allocated. Otherwise a forward search is made for a free block; within
509 * each block group the search first looks for an entire free byte in the block
510 * bitmap, and then for any free bit if that fails.
511 * This function also updates quota and i_blocks field.
514 ext3_new_block(handle_t *handle, struct inode *inode, unsigned long goal,
515 u32 *prealloc_count, u32 *prealloc_block, int *errp)
517 struct buffer_head *bitmap_bh = NULL; /* bh */
518 struct buffer_head *gdp_bh; /* bh2 */
519 int group_no; /* i */
520 int ret_block; /* j */
521 int bgi; /* blockgroup iteration index */
522 int target_block; /* tmp */
524 int performed_allocation = 0;
526 struct super_block *sb;
527 struct ext3_group_desc *gdp;
528 struct ext3_super_block *es;
529 struct ext3_sb_info *sbi;
531 static int goal_hits, goal_attempts;
536 printk("ext3_new_block: nonexistent device");
541 * Check quota for allocation of this block.
543 if (DQUOT_ALLOC_BLOCK(inode, 1)) {
547 if (DLIMIT_ALLOC_BLOCK(sb, inode->i_xid, 1))
551 es = EXT3_SB(sb)->s_es;
552 ext3_debug("goal=%lu.\n", goal);
554 if (!ext3_has_free_blocks(sb)) {
560 * First, test whether the goal block is free.
562 if (goal < le32_to_cpu(es->s_first_data_block) ||
563 goal >= le32_to_cpu(es->s_blocks_count))
564 goal = le32_to_cpu(es->s_first_data_block);
565 group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
566 EXT3_BLOCKS_PER_GROUP(sb);
567 gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
571 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
572 if (free_blocks > 0) {
573 ret_block = ((goal - le32_to_cpu(es->s_first_data_block)) %
574 EXT3_BLOCKS_PER_GROUP(sb));
575 bitmap_bh = read_block_bitmap(sb, group_no);
578 ret_block = ext3_try_to_allocate(sb, handle, group_no,
579 bitmap_bh, ret_block, &fatal);
587 * Now search the rest of the groups. We assume that
588 * i and gdp correctly point to the last group visited.
590 for (bgi = 0; bgi < EXT3_SB(sb)->s_groups_count; bgi++) {
592 if (group_no >= EXT3_SB(sb)->s_groups_count)
594 gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
599 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
600 if (free_blocks <= 0)
604 bitmap_bh = read_block_bitmap(sb, group_no);
607 ret_block = ext3_try_to_allocate(sb, handle, group_no,
608 bitmap_bh, -1, &fatal);
615 /* No space left on the device */
621 ext3_debug("using block group %d(%d)\n",
622 group_no, gdp->bg_free_blocks_count);
624 BUFFER_TRACE(gdp_bh, "get_write_access");
625 fatal = ext3_journal_get_write_access(handle, gdp_bh);
629 target_block = ret_block + group_no * EXT3_BLOCKS_PER_GROUP(sb)
630 + le32_to_cpu(es->s_first_data_block);
632 if (target_block == le32_to_cpu(gdp->bg_block_bitmap) ||
633 target_block == le32_to_cpu(gdp->bg_inode_bitmap) ||
634 in_range(target_block, le32_to_cpu(gdp->bg_inode_table),
635 EXT3_SB(sb)->s_itb_per_group))
636 ext3_error(sb, "ext3_new_block",
637 "Allocating block in system zone - "
638 "block = %u", target_block);
640 performed_allocation = 1;
642 #ifdef CONFIG_JBD_DEBUG
644 struct buffer_head *debug_bh;
646 /* Record bitmap buffer state in the newly allocated block */
647 debug_bh = sb_find_get_block(sb, target_block);
649 BUFFER_TRACE(debug_bh, "state when allocated");
650 BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state");
654 jbd_lock_bh_state(bitmap_bh);
655 spin_lock(sb_bgl_lock(sbi, group_no));
656 if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) {
657 if (ext3_test_bit(ret_block,
658 bh2jh(bitmap_bh)->b_committed_data)) {
659 printk("%s: block was unexpectedly set in "
660 "b_committed_data\n", __FUNCTION__);
663 ext3_debug("found bit %d\n", ret_block);
664 spin_unlock(sb_bgl_lock(sbi, group_no));
665 jbd_unlock_bh_state(bitmap_bh);
668 /* ret_block was blockgroup-relative. Now it becomes fs-relative */
669 ret_block = target_block;
671 if (ret_block >= le32_to_cpu(es->s_blocks_count)) {
672 ext3_error(sb, "ext3_new_block",
673 "block(%d) >= blocks count(%d) - "
674 "block_group = %d, es == %p ", ret_block,
675 le32_to_cpu(es->s_blocks_count), group_no, es);
680 * It is up to the caller to add the new buffer to a journal
681 * list of some description. We don't know in advance whether
682 * the caller wants to use it as metadata or data.
684 ext3_debug("allocating block %d. Goal hits %d of %d.\n",
685 ret_block, goal_hits, goal_attempts);
687 spin_lock(sb_bgl_lock(sbi, group_no));
688 gdp->bg_free_blocks_count =
689 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) - 1);
690 spin_unlock(sb_bgl_lock(sbi, group_no));
691 percpu_counter_mod(&sbi->s_freeblocks_counter, -1);
693 BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
694 err = ext3_journal_dirty_metadata(handle, gdp_bh);
709 DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
713 ext3_std_error(sb, fatal);
716 * Undo the block allocation
718 if (!performed_allocation) {
719 DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
720 DQUOT_FREE_BLOCK(inode, 1);
726 unsigned long ext3_count_free_blocks(struct super_block *sb)
728 unsigned long desc_count;
729 struct ext3_group_desc *gdp;
732 struct ext3_super_block *es;
733 unsigned long bitmap_count, x;
734 struct buffer_head *bitmap_bh = NULL;
737 es = EXT3_SB(sb)->s_es;
741 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
742 gdp = ext3_get_group_desc(sb, i, NULL);
745 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
747 bitmap_bh = read_block_bitmap(sb, i);
748 if (bitmap_bh == NULL)
751 x = ext3_count_free(bitmap_bh, sb->s_blocksize);
752 printk("group %d: stored = %d, counted = %lu\n",
753 i, le16_to_cpu(gdp->bg_free_blocks_count), x);
757 printk("ext3_count_free_blocks: stored = %u, computed = %lu, %lu\n",
758 le32_to_cpu(es->s_free_blocks_count), desc_count, bitmap_count);
763 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
764 gdp = ext3_get_group_desc(sb, i, NULL);
767 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
774 static inline int block_in_use(unsigned long block,
775 struct super_block * sb,
778 return ext3_test_bit ((block -
779 le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block)) %
780 EXT3_BLOCKS_PER_GROUP(sb), map);
783 static inline int test_root(int a, int b)
796 int ext3_group_sparse(int group)
798 return (test_root(group, 3) || test_root(group, 5) ||
799 test_root(group, 7));
803 * ext3_bg_has_super - number of blocks used by the superblock in group
804 * @sb: superblock for filesystem
805 * @group: group number to check
807 * Return the number of blocks used by the superblock (primary or backup)
808 * in this group. Currently this will be only 0 or 1.
810 int ext3_bg_has_super(struct super_block *sb, int group)
812 if (EXT3_HAS_RO_COMPAT_FEATURE(sb,EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
813 !ext3_group_sparse(group))
819 * ext3_bg_num_gdb - number of blocks used by the group table in group
820 * @sb: superblock for filesystem
821 * @group: group number to check
823 * Return the number of blocks used by the group descriptor table
824 * (primary or backup) in this group. In the future there may be a
825 * different number of descriptor blocks in each group.
827 unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
829 if (EXT3_HAS_RO_COMPAT_FEATURE(sb,EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
830 !ext3_group_sparse(group))
832 return EXT3_SB(sb)->s_gdb_count;
835 #ifdef CONFIG_EXT3_CHECK
836 /* Called at mount-time, super-block is locked */
837 void ext3_check_blocks_bitmap (struct super_block * sb)
839 struct ext3_super_block *es;
840 unsigned long desc_count, bitmap_count, x, j;
841 unsigned long desc_blocks;
842 struct buffer_head *bitmap_bh = NULL;
843 struct ext3_group_desc *gdp;
846 es = EXT3_SB(sb)->s_es;
850 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
851 gdp = ext3_get_group_desc (sb, i, NULL);
854 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
856 bitmap_bh = read_block_bitmap(sb, i);
857 if (bitmap_bh == NULL)
860 if (ext3_bg_has_super(sb, i) &&
861 !ext3_test_bit(0, bitmap_bh->b_data))
862 ext3_error(sb, __FUNCTION__,
863 "Superblock in group %d is marked free", i);
865 desc_blocks = ext3_bg_num_gdb(sb, i);
866 for (j = 0; j < desc_blocks; j++)
867 if (!ext3_test_bit(j + 1, bitmap_bh->b_data))
868 ext3_error(sb, __FUNCTION__,
869 "Descriptor block #%ld in group "
870 "%d is marked free", j, i);
872 if (!block_in_use (le32_to_cpu(gdp->bg_block_bitmap),
873 sb, bitmap_bh->b_data))
874 ext3_error (sb, "ext3_check_blocks_bitmap",
875 "Block bitmap for group %d is marked free",
878 if (!block_in_use (le32_to_cpu(gdp->bg_inode_bitmap),
879 sb, bitmap_bh->b_data))
880 ext3_error (sb, "ext3_check_blocks_bitmap",
881 "Inode bitmap for group %d is marked free",
884 for (j = 0; j < EXT3_SB(sb)->s_itb_per_group; j++)
885 if (!block_in_use (le32_to_cpu(gdp->bg_inode_table) + j,
886 sb, bitmap_bh->b_data))
887 ext3_error (sb, "ext3_check_blocks_bitmap",
888 "Block #%d of the inode table in "
889 "group %d is marked free", j, i);
891 x = ext3_count_free(bitmap_bh, sb->s_blocksize);
892 if (le16_to_cpu(gdp->bg_free_blocks_count) != x)
893 ext3_error (sb, "ext3_check_blocks_bitmap",
894 "Wrong free blocks count for group %d, "
895 "stored = %d, counted = %lu", i,
896 le16_to_cpu(gdp->bg_free_blocks_count), x);
900 if (le32_to_cpu(es->s_free_blocks_count) != bitmap_count)
901 ext3_error (sb, "ext3_check_blocks_bitmap",
902 "Wrong free blocks count in super block, "
903 "stored = %lu, counted = %lu",
904 (unsigned long)le32_to_cpu(es->s_free_blocks_count),