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);
472 * ext3_new_block uses a goal block to assist allocation. If the goal is
473 * free, or there is a free block within 32 blocks of the goal, that block
474 * is allocated. Otherwise a forward search is made for a free block; within
475 * each block group the search first looks for an entire free byte in the block
476 * bitmap, and then for any free bit if that fails.
477 * This function also updates quota and i_blocks field.
480 ext3_new_block(handle_t *handle, struct inode *inode, unsigned long goal,
481 u32 *prealloc_count, u32 *prealloc_block, int *errp)
483 struct buffer_head *bitmap_bh = NULL; /* bh */
484 struct buffer_head *gdp_bh; /* bh2 */
485 int group_no; /* i */
486 int ret_block; /* j */
487 int bgi; /* blockgroup iteration index */
488 int target_block; /* tmp */
490 int performed_allocation = 0;
491 int free_blocks, root_blocks;
492 struct super_block *sb;
493 struct ext3_group_desc *gdp;
494 struct ext3_super_block *es;
495 struct ext3_sb_info *sbi;
497 static int goal_hits, goal_attempts;
502 printk("ext3_new_block: nonexistent device");
507 * Check quota for allocation of this block.
509 if (DQUOT_ALLOC_BLOCK(inode, 1)) {
513 if (DLIMIT_ALLOC_BLOCK(sb, inode->i_xid, 1))
517 es = EXT3_SB(sb)->s_es;
518 ext3_debug("goal=%lu.\n", goal);
520 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
521 root_blocks = le32_to_cpu(es->s_r_blocks_count);
523 DLIMIT_ADJUST_BLOCK(sb, vx_current_xid(), &free_blocks, &root_blocks);
525 if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
526 sbi->s_resuid != current->fsuid &&
527 (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
533 * First, test whether the goal block is free.
535 if (goal < le32_to_cpu(es->s_first_data_block) ||
536 goal >= le32_to_cpu(es->s_blocks_count))
537 goal = le32_to_cpu(es->s_first_data_block);
538 group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
539 EXT3_BLOCKS_PER_GROUP(sb);
540 gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
544 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
545 if (free_blocks > 0) {
546 ret_block = ((goal - le32_to_cpu(es->s_first_data_block)) %
547 EXT3_BLOCKS_PER_GROUP(sb));
548 bitmap_bh = read_block_bitmap(sb, group_no);
551 ret_block = ext3_try_to_allocate(sb, handle, group_no,
552 bitmap_bh, ret_block, &fatal);
560 * Now search the rest of the groups. We assume that
561 * i and gdp correctly point to the last group visited.
563 for (bgi = 0; bgi < EXT3_SB(sb)->s_groups_count; bgi++) {
565 if (group_no >= EXT3_SB(sb)->s_groups_count)
567 gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
572 free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
573 if (free_blocks <= 0)
577 bitmap_bh = read_block_bitmap(sb, group_no);
580 ret_block = ext3_try_to_allocate(sb, handle, group_no,
581 bitmap_bh, -1, &fatal);
588 /* No space left on the device */
594 ext3_debug("using block group %d(%d)\n",
595 group_no, gdp->bg_free_blocks_count);
597 BUFFER_TRACE(gdp_bh, "get_write_access");
598 fatal = ext3_journal_get_write_access(handle, gdp_bh);
602 target_block = ret_block + group_no * EXT3_BLOCKS_PER_GROUP(sb)
603 + le32_to_cpu(es->s_first_data_block);
605 if (target_block == le32_to_cpu(gdp->bg_block_bitmap) ||
606 target_block == le32_to_cpu(gdp->bg_inode_bitmap) ||
607 in_range(target_block, le32_to_cpu(gdp->bg_inode_table),
608 EXT3_SB(sb)->s_itb_per_group))
609 ext3_error(sb, "ext3_new_block",
610 "Allocating block in system zone - "
611 "block = %u", target_block);
613 performed_allocation = 1;
615 #ifdef CONFIG_JBD_DEBUG
617 struct buffer_head *debug_bh;
619 /* Record bitmap buffer state in the newly allocated block */
620 debug_bh = sb_find_get_block(sb, target_block);
622 BUFFER_TRACE(debug_bh, "state when allocated");
623 BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state");
627 jbd_lock_bh_state(bitmap_bh);
628 spin_lock(sb_bgl_lock(sbi, group_no));
629 if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) {
630 if (ext3_test_bit(ret_block,
631 bh2jh(bitmap_bh)->b_committed_data)) {
632 printk("%s: block was unexpectedly set in "
633 "b_committed_data\n", __FUNCTION__);
636 ext3_debug("found bit %d\n", ret_block);
637 spin_unlock(sb_bgl_lock(sbi, group_no));
638 jbd_unlock_bh_state(bitmap_bh);
641 /* ret_block was blockgroup-relative. Now it becomes fs-relative */
642 ret_block = target_block;
644 if (ret_block >= le32_to_cpu(es->s_blocks_count)) {
645 ext3_error(sb, "ext3_new_block",
646 "block(%d) >= blocks count(%d) - "
647 "block_group = %d, es == %p ", ret_block,
648 le32_to_cpu(es->s_blocks_count), group_no, es);
653 * It is up to the caller to add the new buffer to a journal
654 * list of some description. We don't know in advance whether
655 * the caller wants to use it as metadata or data.
657 ext3_debug("allocating block %d. Goal hits %d of %d.\n",
658 ret_block, goal_hits, goal_attempts);
660 spin_lock(sb_bgl_lock(sbi, group_no));
661 gdp->bg_free_blocks_count =
662 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) - 1);
663 spin_unlock(sb_bgl_lock(sbi, group_no));
664 percpu_counter_mod(&sbi->s_freeblocks_counter, -1);
666 BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
667 err = ext3_journal_dirty_metadata(handle, gdp_bh);
682 DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
686 ext3_std_error(sb, fatal);
689 * Undo the block allocation
691 if (!performed_allocation) {
692 DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
693 DQUOT_FREE_BLOCK(inode, 1);
699 unsigned long ext3_count_free_blocks(struct super_block *sb)
701 unsigned long desc_count;
702 struct ext3_group_desc *gdp;
705 struct ext3_super_block *es;
706 unsigned long bitmap_count, x;
707 struct buffer_head *bitmap_bh = NULL;
710 es = EXT3_SB(sb)->s_es;
714 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
715 gdp = ext3_get_group_desc(sb, i, NULL);
718 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
720 bitmap_bh = read_block_bitmap(sb, i);
721 if (bitmap_bh == NULL)
724 x = ext3_count_free(bitmap_bh, sb->s_blocksize);
725 printk("group %d: stored = %d, counted = %lu\n",
726 i, le16_to_cpu(gdp->bg_free_blocks_count), x);
730 printk("ext3_count_free_blocks: stored = %u, computed = %lu, %lu\n",
731 le32_to_cpu(es->s_free_blocks_count), desc_count, bitmap_count);
736 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
737 gdp = ext3_get_group_desc(sb, i, NULL);
740 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
747 static inline int block_in_use(unsigned long block,
748 struct super_block * sb,
751 return ext3_test_bit ((block -
752 le32_to_cpu(EXT3_SB(sb)->s_es->s_first_data_block)) %
753 EXT3_BLOCKS_PER_GROUP(sb), map);
756 static inline int test_root(int a, int b)
769 int ext3_group_sparse(int group)
771 return (test_root(group, 3) || test_root(group, 5) ||
772 test_root(group, 7));
776 * ext3_bg_has_super - number of blocks used by the superblock in group
777 * @sb: superblock for filesystem
778 * @group: group number to check
780 * Return the number of blocks used by the superblock (primary or backup)
781 * in this group. Currently this will be only 0 or 1.
783 int ext3_bg_has_super(struct super_block *sb, int group)
785 if (EXT3_HAS_RO_COMPAT_FEATURE(sb,EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
786 !ext3_group_sparse(group))
792 * ext3_bg_num_gdb - number of blocks used by the group table in group
793 * @sb: superblock for filesystem
794 * @group: group number to check
796 * Return the number of blocks used by the group descriptor table
797 * (primary or backup) in this group. In the future there may be a
798 * different number of descriptor blocks in each group.
800 unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
802 if (EXT3_HAS_RO_COMPAT_FEATURE(sb,EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
803 !ext3_group_sparse(group))
805 return EXT3_SB(sb)->s_gdb_count;
808 #ifdef CONFIG_EXT3_CHECK
809 /* Called at mount-time, super-block is locked */
810 void ext3_check_blocks_bitmap (struct super_block * sb)
812 struct ext3_super_block *es;
813 unsigned long desc_count, bitmap_count, x, j;
814 unsigned long desc_blocks;
815 struct buffer_head *bitmap_bh = NULL;
816 struct ext3_group_desc *gdp;
819 es = EXT3_SB(sb)->s_es;
823 for (i = 0; i < EXT3_SB(sb)->s_groups_count; i++) {
824 gdp = ext3_get_group_desc (sb, i, NULL);
827 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
829 bitmap_bh = read_block_bitmap(sb, i);
830 if (bitmap_bh == NULL)
833 if (ext3_bg_has_super(sb, i) &&
834 !ext3_test_bit(0, bitmap_bh->b_data))
835 ext3_error(sb, __FUNCTION__,
836 "Superblock in group %d is marked free", i);
838 desc_blocks = ext3_bg_num_gdb(sb, i);
839 for (j = 0; j < desc_blocks; j++)
840 if (!ext3_test_bit(j + 1, bitmap_bh->b_data))
841 ext3_error(sb, __FUNCTION__,
842 "Descriptor block #%ld in group "
843 "%d is marked free", j, i);
845 if (!block_in_use (le32_to_cpu(gdp->bg_block_bitmap),
846 sb, bitmap_bh->b_data))
847 ext3_error (sb, "ext3_check_blocks_bitmap",
848 "Block bitmap for group %d is marked free",
851 if (!block_in_use (le32_to_cpu(gdp->bg_inode_bitmap),
852 sb, bitmap_bh->b_data))
853 ext3_error (sb, "ext3_check_blocks_bitmap",
854 "Inode bitmap for group %d is marked free",
857 for (j = 0; j < EXT3_SB(sb)->s_itb_per_group; j++)
858 if (!block_in_use (le32_to_cpu(gdp->bg_inode_table) + j,
859 sb, bitmap_bh->b_data))
860 ext3_error (sb, "ext3_check_blocks_bitmap",
861 "Block #%d of the inode table in "
862 "group %d is marked free", j, i);
864 x = ext3_count_free(bitmap_bh, sb->s_blocksize);
865 if (le16_to_cpu(gdp->bg_free_blocks_count) != x)
866 ext3_error (sb, "ext3_check_blocks_bitmap",
867 "Wrong free blocks count for group %d, "
868 "stored = %d, counted = %lu", i,
869 le16_to_cpu(gdp->bg_free_blocks_count), x);
873 if (le32_to_cpu(es->s_free_blocks_count) != bitmap_count)
874 ext3_error (sb, "ext3_check_blocks_bitmap",
875 "Wrong free blocks count in super block, "
876 "stored = %lu, counted = %lu",
877 (unsigned long)le32_to_cpu(es->s_free_blocks_count),