cleanup for tfs
[thunix.git] / fs / tfs / balloc.c
blob206a3621845076170578a3cdcf1aac29e603281a
1 #include <stdio.h>
2 #include <malloc.h>
3 #include <bitopts.h>
4 #include <tfs.h>
6 static void * tfs_read_block_bitmap(struct tfs_sb_info *sbi)
8 char *buf = malloc(sbi->s_block_size);
10 tfs_bread(sbi, sbi->s_block_bitmap, buf);
12 return buf;
16 * Free a block, return -1 if failed, or return 0
18 int tfs_free_block(struct tfs_sb_info *sbi, uint32_t block)
20 char *bitmap = tfs_read_block_bitmap(sbi);
22 if (clear_bit(bitmap, block) == 0) {
23 printk("ERROR: trying to free an free block!\n");
24 free(bitmap);
25 return -1;
28 tfs_bwrite(sbi, sbi->s_block_bitmap, bitmap);
29 free(bitmap);
30 return block;
33 int tfs_alloc_block(struct tfs_sb_info *sbi, uint32_t block)
35 char *bitmap = tfs_read_block_bitmap(sbi);
37 /* try the target first */
38 if (test_bit(bitmap, block) != 0)
39 block = find_first_zero(bitmap, bitmap + sbi->s_block_size);
40 if (block != -1) {
41 set_bit(bitmap, block);
42 tfs_bwrite(sbi, sbi->s_block_bitmap, bitmap);
45 free(bitmap);
46 return block;