Cleanup the warning messages
[thunix.git] / fs / balloc.c
blobebd5b9a22cff8d50a9f55362c62a46a22597fd80
1 /*
2 * thunix/fs/balloc.c
3 * it contains the blocks allocation and deallocation routines
4 */
6 #include <fs_ext2.h>
8 #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
10 struct ext2_group_desc * ext2_get_group_desc(unsigned int block_group)
12 struct ext2_group_desc * desc;
13 /* struct ext2_sb_info *sbi = EXT2_SBI();
16 if (block_group >= sbi->s_groups_count) {
17 ext2_error ("ext2_get_group_desc",
18 "block_group >= groups_count - "
19 "block_group = %d, groups_count = %lu",
20 block_group, sbi->s_groups_count);
22 return NULL;
26 desc = (struct ext2_group_desc *) EXT2_GROUP_DESC_BUFFER;
28 return desc + block_group;
34 * Read the bitmap for a given block_group, reading into the specified
35 * slot in the superblock's bitmap cache.
38 void * ext2_read_block_bitmap(unsigned int block_group)
40 struct ext2_group_desc * desc;
41 void * bitmap = NULL;
43 desc = ext2_get_group_desc (block_group);
44 if (!desc)
45 goto error_out;
46 bitmap = (void *)(EXT2_BITMAP_BUFFER + (block_group << 1) * EXT2_BLOCK_SIZE);
47 if (!bitmap)
48 ext2_error ("read_block_bitmap",
49 "Cannot read block bitmap - "
50 "block_group = %d, block_bitmap = %u",
51 block_group, desc->bg_block_bitmap);
52 error_out:
53 return bitmap;
57 /* free the goal block, clean it */
58 void ext2_free_block(unsigned int block)
60 unsigned int block_group;
61 unsigned int bit;
62 struct ext2_group_desc *desc;
63 struct ext2_sb_info * sbi = EXT2_SBI();
64 void * bitmap;
66 block_group = ext2_get_group_num (block, BLOCK);
67 bit = ext2_get_group_offset (block, BLOCK);
69 desc = ext2_get_group_desc (block_group);
70 bitmap = ext2_read_block_bitmap (block_group);
73 if ( !ext2_clear_bit(bitmap, bit) )
74 ext2_error("bit %d (%d) alread cleard", bit, block_group);
76 desc->bg_free_blocks_count ++;
77 sbi->s_free_blocks_count ++;
81 * get the specified goal block
82 * if alread alloced find it by looking forward
84 int ext2_grab_block( char *bitmap, unsigned int goal)
86 if ( !ext2_test_bit(bitmap, goal) )
87 goto got_block;
89 /* else looking forward */
90 goal = find_first_zero (bitmap, goal + 1, EXT2_BLOCKS_PER_GROUP);
91 got_block:
92 return goal;
95 /* alloc a new block */
96 int ext2_alloc_block ( unsigned int goal)
98 unsigned int block;
99 unsigned int block_group;
100 unsigned int bit;
101 struct ext2_group_desc *desc;
102 struct ext2_sb_info * sbi = EXT2_SBI();
103 void *bitmap;
105 block_group = ext2_get_group_num (goal, BLOCK);
106 bit = ext2_get_group_offset (goal, BLOCK);
108 bitmap = ext2_read_block_bitmap (block_group);
109 block = ext2_grab_block (bitmap, bit);
111 if ( !block)
112 ext2_error ("no free blocks any more");
114 desc = ext2_get_group_desc (block_group);
115 desc->bg_free_blocks_count --;
116 sbi->s_free_blocks_count --;
117 ext2_set_bit (bitmap, block);
119 return block;