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/sched.h>
17 #include <linux/jbd.h>
18 #include <linux/ext3_fs.h>
19 #include <linux/ext3_jbd.h>
20 #include <linux/locks.h>
21 #include <linux/quotaops.h>
24 * balloc.c contains the blocks allocation and deallocation routines
28 * The free blocks are managed by bitmaps. A file system contains several
29 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
30 * block for inodes, N blocks for the inode table and data blocks.
32 * The file system contains group descriptors which are located after the
33 * super block. Each descriptor contains the number of the bitmap block and
34 * the free blocks count in the block. The descriptors are loaded in memory
35 * when a file system is mounted (see ext3_read_super).
39 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
41 struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
42 unsigned int block_group,
43 struct buffer_head ** bh)
45 unsigned long group_desc;
47 struct ext3_group_desc * gdp;
49 if (block_group >= sb->u.ext3_sb.s_groups_count) {
50 ext3_error (sb, "ext3_get_group_desc",
51 "block_group >= groups_count - "
52 "block_group = %d, groups_count = %lu",
53 block_group, sb->u.ext3_sb.s_groups_count);
58 group_desc = block_group / EXT3_DESC_PER_BLOCK(sb);
59 desc = block_group % EXT3_DESC_PER_BLOCK(sb);
60 if (!sb->u.ext3_sb.s_group_desc[group_desc]) {
61 ext3_error (sb, "ext3_get_group_desc",
62 "Group descriptor not loaded - "
63 "block_group = %d, group_desc = %lu, desc = %lu",
64 block_group, group_desc, desc);
68 gdp = (struct ext3_group_desc *)
69 sb->u.ext3_sb.s_group_desc[group_desc]->b_data;
71 *bh = sb->u.ext3_sb.s_group_desc[group_desc];
76 * Read the bitmap for a given block_group, reading into the specified
77 * slot in the superblock's bitmap cache.
79 * Return >=0 on success or a -ve error code.
82 static int read_block_bitmap (struct super_block * sb,
83 unsigned int block_group,
84 unsigned long bitmap_nr)
86 struct ext3_group_desc * gdp;
87 struct buffer_head * bh = NULL;
90 gdp = ext3_get_group_desc (sb, block_group, NULL);
94 bh = sb_bread(sb, le32_to_cpu(gdp->bg_block_bitmap));
96 ext3_error (sb, "read_block_bitmap",
97 "Cannot read block bitmap - "
98 "block_group = %d, block_bitmap = %lu",
99 block_group, (unsigned long) gdp->bg_block_bitmap);
103 * On IO error, just leave a zero in the superblock's block pointer for
104 * this group. The IO will be retried next time.
107 sb->u.ext3_sb.s_block_bitmap_number[bitmap_nr] = block_group;
108 sb->u.ext3_sb.s_block_bitmap[bitmap_nr] = bh;
113 * load_block_bitmap loads the block bitmap for a blocks group
115 * It maintains a cache for the last bitmaps loaded. This cache is managed
116 * with a LRU algorithm.
119 * 1/ There is one cache per mounted file system.
120 * 2/ If the file system contains less than EXT3_MAX_GROUP_LOADED groups,
121 * this function reads the bitmap without maintaining a LRU cache.
123 * Return the slot used to store the bitmap, or a -ve error code.
125 static int __load_block_bitmap (struct super_block * sb,
126 unsigned int block_group)
128 int i, j, retval = 0;
129 unsigned long block_bitmap_number;
130 struct buffer_head * block_bitmap;
132 if (block_group >= sb->u.ext3_sb.s_groups_count)
133 ext3_panic (sb, "load_block_bitmap",
134 "block_group >= groups_count - "
135 "block_group = %d, groups_count = %lu",
136 block_group, sb->u.ext3_sb.s_groups_count);
138 if (sb->u.ext3_sb.s_groups_count <= EXT3_MAX_GROUP_LOADED) {
139 if (sb->u.ext3_sb.s_block_bitmap[block_group]) {
140 if (sb->u.ext3_sb.s_block_bitmap_number[block_group] ==
143 ext3_error (sb, "__load_block_bitmap",
144 "block_group != block_bitmap_number");
146 retval = read_block_bitmap (sb, block_group, block_group);
152 for (i = 0; i < sb->u.ext3_sb.s_loaded_block_bitmaps &&
153 sb->u.ext3_sb.s_block_bitmap_number[i] != block_group; i++)
155 if (i < sb->u.ext3_sb.s_loaded_block_bitmaps &&
156 sb->u.ext3_sb.s_block_bitmap_number[i] == block_group) {
157 block_bitmap_number = sb->u.ext3_sb.s_block_bitmap_number[i];
158 block_bitmap = sb->u.ext3_sb.s_block_bitmap[i];
159 for (j = i; j > 0; j--) {
160 sb->u.ext3_sb.s_block_bitmap_number[j] =
161 sb->u.ext3_sb.s_block_bitmap_number[j - 1];
162 sb->u.ext3_sb.s_block_bitmap[j] =
163 sb->u.ext3_sb.s_block_bitmap[j - 1];
165 sb->u.ext3_sb.s_block_bitmap_number[0] = block_bitmap_number;
166 sb->u.ext3_sb.s_block_bitmap[0] = block_bitmap;
169 * There's still one special case here --- if block_bitmap == 0
170 * then our last attempt to read the bitmap failed and we have
171 * just ended up caching that failure. Try again to read it.
174 retval = read_block_bitmap (sb, block_group, 0);
176 if (sb->u.ext3_sb.s_loaded_block_bitmaps<EXT3_MAX_GROUP_LOADED)
177 sb->u.ext3_sb.s_loaded_block_bitmaps++;
179 brelse (sb->u.ext3_sb.s_block_bitmap
180 [EXT3_MAX_GROUP_LOADED - 1]);
181 for (j = sb->u.ext3_sb.s_loaded_block_bitmaps - 1;
183 sb->u.ext3_sb.s_block_bitmap_number[j] =
184 sb->u.ext3_sb.s_block_bitmap_number[j - 1];
185 sb->u.ext3_sb.s_block_bitmap[j] =
186 sb->u.ext3_sb.s_block_bitmap[j - 1];
188 retval = read_block_bitmap (sb, block_group, 0);
194 * Load the block bitmap for a given block group. First of all do a couple
195 * of fast lookups for common cases and then pass the request onto the guts
196 * of the bitmap loader.
198 * Return the slot number of the group in the superblock bitmap cache's on
199 * success, or a -ve error code.
201 * There is still one inconsistency here --- if the number of groups in this
202 * filesystems is <= EXT3_MAX_GROUP_LOADED, then we have no way of
203 * differentiating between a group for which we have never performed a bitmap
204 * IO request, and a group for which the last bitmap read request failed.
206 static inline int load_block_bitmap (struct super_block * sb,
207 unsigned int block_group)
212 * Do the lookup for the slot. First of all, check if we're asking
213 * for the same slot as last time, and did we succeed that last time?
215 if (sb->u.ext3_sb.s_loaded_block_bitmaps > 0 &&
216 sb->u.ext3_sb.s_block_bitmap_number[0] == block_group &&
217 sb->u.ext3_sb.s_block_bitmap[0]) {
221 * Or can we do a fast lookup based on a loaded group on a filesystem
222 * small enough to be mapped directly into the superblock?
224 else if (sb->u.ext3_sb.s_groups_count <= EXT3_MAX_GROUP_LOADED &&
225 sb->u.ext3_sb.s_block_bitmap_number[block_group]==block_group
226 && sb->u.ext3_sb.s_block_bitmap[block_group]) {
230 * If not, then do a full lookup for this block group.
233 slot = __load_block_bitmap (sb, block_group);
237 * <0 means we just got an error
243 * If it's a valid slot, we may still have cached a previous IO error,
244 * in which case the bh in the superblock cache will be zero.
246 if (!sb->u.ext3_sb.s_block_bitmap[slot])
250 * Must have been read in OK to get this far.
255 /* Free given blocks, update quota and i_blocks field */
256 void ext3_free_blocks (handle_t *handle, struct inode * inode,
257 unsigned long block, unsigned long count)
259 struct buffer_head *bitmap_bh;
260 struct buffer_head *gd_bh;
261 unsigned long block_group;
265 unsigned long overflow;
266 struct super_block * sb;
267 struct ext3_group_desc * gdp;
268 struct ext3_super_block * es;
270 int dquot_freed_blocks = 0;
274 printk ("ext3_free_blocks: nonexistent device");
278 es = sb->u.ext3_sb.s_es;
279 if (block < le32_to_cpu(es->s_first_data_block) ||
280 (block + count) > le32_to_cpu(es->s_blocks_count)) {
281 ext3_error (sb, "ext3_free_blocks",
282 "Freeing blocks not in datazone - "
283 "block = %lu, count = %lu", block, count);
287 ext3_debug ("freeing block %lu\n", block);
291 block_group = (block - le32_to_cpu(es->s_first_data_block)) /
292 EXT3_BLOCKS_PER_GROUP(sb);
293 bit = (block - le32_to_cpu(es->s_first_data_block)) %
294 EXT3_BLOCKS_PER_GROUP(sb);
296 * Check to see if we are freeing blocks across a group
299 if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
300 overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
303 bitmap_nr = load_block_bitmap (sb, block_group);
307 bitmap_bh = sb->u.ext3_sb.s_block_bitmap[bitmap_nr];
308 gdp = ext3_get_group_desc (sb, block_group, &gd_bh);
312 if (in_range (le32_to_cpu(gdp->bg_block_bitmap), block, count) ||
313 in_range (le32_to_cpu(gdp->bg_inode_bitmap), block, count) ||
314 in_range (block, le32_to_cpu(gdp->bg_inode_table),
315 sb->u.ext3_sb.s_itb_per_group) ||
316 in_range (block + count - 1, le32_to_cpu(gdp->bg_inode_table),
317 sb->u.ext3_sb.s_itb_per_group))
318 ext3_error (sb, "ext3_free_blocks",
319 "Freeing blocks in system zones - "
320 "Block = %lu, count = %lu",
324 * We are about to start releasing blocks in the bitmap,
325 * so we need undo access.
327 /* @@@ check errors */
328 BUFFER_TRACE(bitmap_bh, "getting undo access");
329 err = ext3_journal_get_undo_access(handle, bitmap_bh);
334 * We are about to modify some metadata. Call the journal APIs
335 * to unshare ->b_data if a currently-committing transaction is
338 BUFFER_TRACE(gd_bh, "get_write_access");
339 err = ext3_journal_get_write_access(handle, gd_bh);
343 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "get_write_access");
344 err = ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
348 for (i = 0; i < count; i++) {
350 * An HJ special. This is expensive...
352 #ifdef CONFIG_JBD_DEBUG
354 struct buffer_head *debug_bh;
355 debug_bh = sb_get_hash_table(sb, block + i);
357 BUFFER_TRACE(debug_bh, "Deleted!");
358 if (!bh2jh(bitmap_bh)->b_committed_data)
359 BUFFER_TRACE(debug_bh,
360 "No commited data in bitmap");
361 BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
366 BUFFER_TRACE(bitmap_bh, "clear bit");
367 if (!ext3_clear_bit (bit + i, bitmap_bh->b_data)) {
368 ext3_error (sb, __FUNCTION__,
369 "bit already cleared for block %lu",
371 BUFFER_TRACE(bitmap_bh, "bit already cleared");
373 dquot_freed_blocks++;
374 gdp->bg_free_blocks_count =
375 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)+1);
376 es->s_free_blocks_count =
377 cpu_to_le32(le32_to_cpu(es->s_free_blocks_count)+1);
379 /* @@@ This prevents newly-allocated data from being
380 * freed and then reallocated within the same
383 * Ideally we would want to allow that to happen, but to
384 * do so requires making journal_forget() capable of
385 * revoking the queued write of a data block, which
386 * implies blocking on the journal lock. *forget()
387 * cannot block due to truncate races.
389 * Eventually we can fix this by making journal_forget()
390 * return a status indicating whether or not it was able
391 * to revoke the buffer. On successful revoke, it is
392 * safe not to set the allocation bit in the committed
393 * bitmap, because we know that there is no outstanding
394 * activity on the buffer any more and so it is safe to
397 BUFFER_TRACE(bitmap_bh, "clear in b_committed_data");
398 J_ASSERT_BH(bitmap_bh,
399 bh2jh(bitmap_bh)->b_committed_data != NULL);
400 ext3_set_bit(bit + i, bh2jh(bitmap_bh)->b_committed_data);
403 /* We dirtied the bitmap block */
404 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
405 err = ext3_journal_dirty_metadata(handle, bitmap_bh);
407 /* And the group descriptor block */
408 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
409 ret = ext3_journal_dirty_metadata(handle, gd_bh);
412 /* And the superblock */
413 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "dirtied superblock");
414 ret = ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
417 if (overflow && !err) {
424 ext3_std_error(sb, err);
426 if (dquot_freed_blocks)
427 DQUOT_FREE_BLOCK(inode, dquot_freed_blocks);
431 /* For ext3 allocations, we must not reuse any blocks which are
432 * allocated in the bitmap buffer's "last committed data" copy. This
433 * prevents deletes from freeing up the page for reuse until we have
434 * committed the delete transaction.
436 * If we didn't do this, then deleting something and reallocating it as
437 * data would allow the old block to be overwritten before the
438 * transaction committed (because we force data to disk before commit).
439 * This would lead to corruption if we crashed between overwriting the
440 * data and committing the delete.
442 * @@@ We may want to make this allocation behaviour conditional on
443 * data-writes at some point, and disable it for metadata allocations or
446 static int ext3_test_allocatable(int nr, struct buffer_head *bh)
448 if (ext3_test_bit(nr, bh->b_data))
450 if (!buffer_jbd(bh) || !bh2jh(bh)->b_committed_data)
452 return !ext3_test_bit(nr, bh2jh(bh)->b_committed_data);
456 * Find an allocatable block in a bitmap. We honour both the bitmap and
457 * its last-committed copy (if that exists), and perform the "most
458 * appropriate allocation" algorithm of looking for a free block near
459 * the initial goal; then for a free byte somewhere in the bitmap; then
460 * for any free bit in the bitmap.
462 static int find_next_usable_block(int start,
463 struct buffer_head *bh, int maxblocks)
470 * The goal was occupied; search forward for a free
471 * block within the next XX blocks.
473 * end_goal is more or less random, but it has to be
474 * less than EXT3_BLOCKS_PER_GROUP. Aligning up to the
475 * next 64-bit boundary is simple..
477 int end_goal = (start + 63) & ~63;
478 here = ext3_find_next_zero_bit(bh->b_data, end_goal, start);
479 if (here < end_goal && ext3_test_allocatable(here, bh))
482 ext3_debug ("Bit not found near goal\n");
491 * There has been no free block found in the near vicinity of
492 * the goal: do a search forward through the block groups,
493 * searching in each group first for an entire free byte in the
494 * bitmap and then for any free bit.
496 * Search first in the remainder of the current group
498 p = ((char *) bh->b_data) + (here >> 3);
499 r = memscan(p, 0, (maxblocks - here + 7) >> 3);
500 next = (r - ((char *) bh->b_data)) << 3;
502 if (next < maxblocks && ext3_test_allocatable(next, bh))
505 /* The bitmap search --- search forward alternately
506 * through the actual bitmap and the last-committed copy
507 * until we find a bit free in both. */
509 while (here < maxblocks) {
510 next = ext3_find_next_zero_bit ((unsigned long *) bh->b_data,
512 if (next >= maxblocks)
514 if (ext3_test_allocatable(next, bh))
517 J_ASSERT_BH(bh, bh2jh(bh)->b_committed_data);
518 here = ext3_find_next_zero_bit
519 ((unsigned long *) bh2jh(bh)->b_committed_data,
526 * ext3_new_block uses a goal block to assist allocation. If the goal is
527 * free, or there is a free block within 32 blocks of the goal, that block
528 * is allocated. Otherwise a forward search is made for a free block; within
529 * each block group the search first looks for an entire free byte in the block
530 * bitmap, and then for any free bit if that fails.
531 * This function also updates quota and i_blocks field.
533 int ext3_new_block (handle_t *handle, struct inode * inode,
534 unsigned long goal, u32 * prealloc_count,
535 u32 * prealloc_block, int * errp)
537 struct buffer_head * bh, *bhtmp;
538 struct buffer_head * bh2;
542 int i, j, k, tmp, alloctmp;
545 int performed_allocation = 0;
546 struct super_block * sb;
547 struct ext3_group_desc * gdp;
548 struct ext3_super_block * es;
550 static int goal_hits = 0, goal_attempts = 0;
555 printk ("ext3_new_block: nonexistent device");
560 * Check quota for allocation of this block.
562 if (DQUOT_ALLOC_BLOCK(inode, 1)) {
568 es = sb->u.ext3_sb.s_es;
569 if (le32_to_cpu(es->s_free_blocks_count) <=
570 le32_to_cpu(es->s_r_blocks_count) &&
571 ((sb->u.ext3_sb.s_resuid != current->fsuid) &&
572 (sb->u.ext3_sb.s_resgid == 0 ||
573 !in_group_p (sb->u.ext3_sb.s_resgid)) &&
574 !capable(CAP_SYS_RESOURCE)))
577 ext3_debug ("goal=%lu.\n", goal);
580 * First, test whether the goal block is free.
582 if (goal < le32_to_cpu(es->s_first_data_block) ||
583 goal >= le32_to_cpu(es->s_blocks_count))
584 goal = le32_to_cpu(es->s_first_data_block);
585 i = (goal - le32_to_cpu(es->s_first_data_block)) /
586 EXT3_BLOCKS_PER_GROUP(sb);
587 gdp = ext3_get_group_desc (sb, i, &bh2);
591 if (le16_to_cpu(gdp->bg_free_blocks_count) > 0) {
592 j = ((goal - le32_to_cpu(es->s_first_data_block)) %
593 EXT3_BLOCKS_PER_GROUP(sb));
598 bitmap_nr = load_block_bitmap (sb, i);
602 bh = sb->u.ext3_sb.s_block_bitmap[bitmap_nr];
604 ext3_debug ("goal is at %d:%d.\n", i, j);
606 if (ext3_test_allocatable(j, bh)) {
609 ext3_debug ("goal bit allocated.\n");
614 j = find_next_usable_block(j, bh, EXT3_BLOCKS_PER_GROUP(sb));
619 ext3_debug ("Bit not found in block group %d.\n", i);
622 * Now search the rest of the groups. We assume that
623 * i and gdp correctly point to the last group visited.
625 for (k = 0; k < sb->u.ext3_sb.s_groups_count; k++) {
627 if (i >= sb->u.ext3_sb.s_groups_count)
629 gdp = ext3_get_group_desc (sb, i, &bh2);
634 if (le16_to_cpu(gdp->bg_free_blocks_count) > 0) {
635 bitmap_nr = load_block_bitmap (sb, i);
639 bh = sb->u.ext3_sb.s_block_bitmap[bitmap_nr];
640 j = find_next_usable_block(-1, bh,
641 EXT3_BLOCKS_PER_GROUP(sb));
647 /* No space left on the device */
652 * We have succeeded in finding a free byte in the block
653 * bitmap. Now search backwards up to 7 bits to find the
654 * start of this group of free blocks.
657 k < 7 && j > 0 && ext3_test_allocatable(j - 1, bh);
663 ext3_debug ("using block group %d(%d)\n", i, gdp->bg_free_blocks_count);
665 /* Make sure we use undo access for the bitmap, because it is
666 critical that we do the frozen_data COW on bitmap buffers in
667 all cases even if the buffer is in BJ_Forget state in the
668 committing transaction. */
669 BUFFER_TRACE(bh, "get undo access for marking new block");
670 fatal = ext3_journal_get_undo_access(handle, bh);
673 BUFFER_TRACE(bh2, "get_write_access");
674 fatal = ext3_journal_get_write_access(handle, bh2);
677 BUFFER_TRACE(sb->u.ext3_sb.s_sbh, "get_write_access");
678 fatal = ext3_journal_get_write_access(handle, sb->u.ext3_sb.s_sbh);
681 tmp = j + i * EXT3_BLOCKS_PER_GROUP(sb)
682 + le32_to_cpu(es->s_first_data_block);
684 if (tmp == le32_to_cpu(gdp->bg_block_bitmap) ||
685 tmp == le32_to_cpu(gdp->bg_inode_bitmap) ||
686 in_range (tmp, le32_to_cpu(gdp->bg_inode_table),
687 sb->u.ext3_sb.s_itb_per_group))
688 ext3_error (sb, "ext3_new_block",
689 "Allocating block in system zone - "
692 /* The superblock lock should guard against anybody else beating
693 * us to this point! */
694 J_ASSERT_BH(bh, !ext3_test_bit(j, bh->b_data));
695 BUFFER_TRACE(bh, "setting bitmap bit");
696 ext3_set_bit(j, bh->b_data);
697 performed_allocation = 1;
699 #ifdef CONFIG_JBD_DEBUG
701 struct buffer_head *debug_bh;
703 /* Record bitmap buffer state in the newly allocated block */
704 debug_bh = sb_get_hash_table(sb, tmp);
706 BUFFER_TRACE(debug_bh, "state when allocated");
707 BUFFER_TRACE2(debug_bh, bh, "bitmap state");
712 if (buffer_jbd(bh) && bh2jh(bh)->b_committed_data)
713 J_ASSERT_BH(bh, !ext3_test_bit(j, bh2jh(bh)->b_committed_data));
717 ext3_debug ("found bit %d\n", j);
720 * Do block preallocation now if required.
722 #ifdef EXT3_PREALLOCATE
724 * akpm: this is not enabled for ext3. Need to use
725 * ext3_test_allocatable()
727 /* Writer: ->i_prealloc* */
728 if (prealloc_count && !*prealloc_count) {
730 unsigned long next_block = tmp + 1;
732 prealloc_goal = es->s_prealloc_blocks ?
733 es->s_prealloc_blocks : EXT3_DEFAULT_PREALLOC_BLOCKS;
735 *prealloc_block = next_block;
738 k < prealloc_goal && (j + k) < EXT3_BLOCKS_PER_GROUP(sb);
740 if (DQUOT_PREALLOC_BLOCK(inode, 1))
742 /* Writer: ->i_prealloc* */
743 if (*prealloc_block + *prealloc_count != next_block ||
744 ext3_set_bit (j + k, bh->b_data)) {
746 DQUOT_FREE_BLOCK(inode, 1);
753 * As soon as we go for per-group spinlocks we'll need these
754 * done inside the loop above.
756 gdp->bg_free_blocks_count =
757 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) -
759 es->s_free_blocks_count =
760 cpu_to_le32(le32_to_cpu(es->s_free_blocks_count) -
762 ext3_debug ("Preallocated a further %lu bits.\n",
769 BUFFER_TRACE(bh, "journal_dirty_metadata for bitmap block");
770 err = ext3_journal_dirty_metadata(handle, bh);
771 if (!fatal) fatal = err;
773 if (j >= le32_to_cpu(es->s_blocks_count)) {
774 ext3_error (sb, "ext3_new_block",
775 "block(%d) >= blocks count(%d) - "
776 "block_group = %d, es == %p ",j,
777 le32_to_cpu(es->s_blocks_count), i, es);
782 * It is up to the caller to add the new buffer to a journal
783 * list of some description. We don't know in advance whether
784 * the caller wants to use it as metadata or data.
787 ext3_debug ("allocating block %d. "
788 "Goal hits %d of %d.\n", j, goal_hits, goal_attempts);
790 gdp->bg_free_blocks_count =
791 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count) - 1);
792 es->s_free_blocks_count =
793 cpu_to_le32(le32_to_cpu(es->s_free_blocks_count) - 1);
795 BUFFER_TRACE(bh2, "journal_dirty_metadata for group descriptor");
796 err = ext3_journal_dirty_metadata(handle, bh2);
797 if (!fatal) fatal = err;
799 BUFFER_TRACE(bh, "journal_dirty_metadata for superblock");
800 err = ext3_journal_dirty_metadata(handle, sb->u.ext3_sb.s_sbh);
801 if (!fatal) fatal = err;
816 ext3_std_error(sb, fatal);
820 * Undo the block allocation
822 if (!performed_allocation)
823 DQUOT_FREE_BLOCK(inode, 1);
828 unsigned long ext3_count_free_blocks (struct super_block * sb)
831 struct ext3_super_block * es;
832 unsigned long desc_count, bitmap_count, x;
834 struct ext3_group_desc * gdp;
838 es = sb->u.ext3_sb.s_es;
842 for (i = 0; i < sb->u.ext3_sb.s_groups_count; i++) {
843 gdp = ext3_get_group_desc (sb, i, NULL);
846 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
847 bitmap_nr = load_block_bitmap (sb, i);
851 x = ext3_count_free (sb->u.ext3_sb.s_block_bitmap[bitmap_nr],
853 printk ("group %d: stored = %d, counted = %lu\n",
854 i, le16_to_cpu(gdp->bg_free_blocks_count), x);
857 printk("ext3_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
858 le32_to_cpu(es->s_free_blocks_count), desc_count, bitmap_count);
862 return le32_to_cpu(sb->u.ext3_sb.s_es->s_free_blocks_count);
866 static inline int block_in_use (unsigned long block,
867 struct super_block * sb,
870 return ext3_test_bit ((block -
871 le32_to_cpu(sb->u.ext3_sb.s_es->s_first_data_block)) %
872 EXT3_BLOCKS_PER_GROUP(sb), map);
875 static inline int test_root(int a, int b)
888 int ext3_group_sparse(int group)
890 return (test_root(group, 3) || test_root(group, 5) ||
891 test_root(group, 7));
895 * ext3_bg_has_super - number of blocks used by the superblock in group
896 * @sb: superblock for filesystem
897 * @group: group number to check
899 * Return the number of blocks used by the superblock (primary or backup)
900 * in this group. Currently this will be only 0 or 1.
902 int ext3_bg_has_super(struct super_block *sb, int group)
904 if (EXT3_HAS_RO_COMPAT_FEATURE(sb,EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
905 !ext3_group_sparse(group))
911 * ext3_bg_num_gdb - number of blocks used by the group table in group
912 * @sb: superblock for filesystem
913 * @group: group number to check
915 * Return the number of blocks used by the group descriptor table
916 * (primary or backup) in this group. In the future there may be a
917 * different number of descriptor blocks in each group.
919 unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
921 if (EXT3_HAS_RO_COMPAT_FEATURE(sb,EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
922 !ext3_group_sparse(group))
924 return EXT3_SB(sb)->s_gdb_count;
927 #ifdef CONFIG_EXT3_CHECK
928 /* Called at mount-time, super-block is locked */
929 void ext3_check_blocks_bitmap (struct super_block * sb)
931 struct buffer_head * bh;
932 struct ext3_super_block * es;
933 unsigned long desc_count, bitmap_count, x, j;
934 unsigned long desc_blocks;
936 struct ext3_group_desc * gdp;
939 es = sb->u.ext3_sb.s_es;
943 for (i = 0; i < sb->u.ext3_sb.s_groups_count; i++) {
944 gdp = ext3_get_group_desc (sb, i, NULL);
947 desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
948 bitmap_nr = load_block_bitmap (sb, i);
952 bh = EXT3_SB(sb)->s_block_bitmap[bitmap_nr];
954 if (ext3_bg_has_super(sb, i) && !ext3_test_bit(0, bh->b_data))
955 ext3_error(sb, __FUNCTION__,
956 "Superblock in group %d is marked free", i);
958 desc_blocks = ext3_bg_num_gdb(sb, i);
959 for (j = 0; j < desc_blocks; j++)
960 if (!ext3_test_bit(j + 1, bh->b_data))
961 ext3_error(sb, __FUNCTION__,
962 "Descriptor block #%ld in group "
963 "%d is marked free", j, i);
965 if (!block_in_use (le32_to_cpu(gdp->bg_block_bitmap),
967 ext3_error (sb, "ext3_check_blocks_bitmap",
968 "Block bitmap for group %d is marked free",
971 if (!block_in_use (le32_to_cpu(gdp->bg_inode_bitmap),
973 ext3_error (sb, "ext3_check_blocks_bitmap",
974 "Inode bitmap for group %d is marked free",
977 for (j = 0; j < sb->u.ext3_sb.s_itb_per_group; j++)
978 if (!block_in_use (le32_to_cpu(gdp->bg_inode_table) + j,
980 ext3_error (sb, "ext3_check_blocks_bitmap",
981 "Block #%d of the inode table in "
982 "group %d is marked free", j, i);
984 x = ext3_count_free (bh, sb->s_blocksize);
985 if (le16_to_cpu(gdp->bg_free_blocks_count) != x)
986 ext3_error (sb, "ext3_check_blocks_bitmap",
987 "Wrong free blocks count for group %d, "
988 "stored = %d, counted = %lu", i,
989 le16_to_cpu(gdp->bg_free_blocks_count), x);
992 if (le32_to_cpu(es->s_free_blocks_count) != bitmap_count)
993 ext3_error (sb, "ext3_check_blocks_bitmap",
994 "Wrong free blocks count in super block, "
995 "stored = %lu, counted = %lu",
996 (unsigned long)le32_to_cpu(es->s_free_blocks_count),