Makefile cleaup
[thunix.git] / fs / tfs / ialloc.c
blob74c6c12965c296c77aaea56bbcb3dfc7ca0f5019
1 #include <stdio.h>
2 #include <malloc.h>
3 #include <bitopts.h>
4 #include <tfs.h>
7 static void * tfs_read_inode_bitmap(struct tfs_sb_info *sbi)
9 char *buf = malloc(sbi->s_block_size);
11 tfs_bread(sbi, sbi->s_inode_bitmap, buf);
13 return buf;
17 * free an inode, return -1 if failed, or return 9
19 int tfs_free_inode(struct tfs_sb_info *sbi, int inr)
21 char *bitmap = tfs_read_inode_bitmap(sbi);
23 /* inode number count from 1 */
24 if (clear_bit(bitmap, inr - 1) == 0) {
25 printk("ERROR: trying to free an unallocated inode!\n");
26 free(bitmap);
27 return -1;
30 tfs_bwrite(sbi, sbi->s_inode_bitmap, bitmap);
31 free(bitmap);
32 return 0;
35 int tfs_alloc_inode(struct tfs_sb_info *sbi, int inr)
37 char *bitmap;
39 if (inr < 0) {
40 printk("ERROR: trying to alloc a negtive inode!\n");
41 return -1;
44 bitmap = tfs_read_inode_bitmap(sbi);
45 /* try the target first */
46 if (test_bit(bitmap, inr - 1) != 0)
47 inr = find_first_zero(bitmap, bitmap + sbi->s_block_size) + 1;
48 if (inr != -1) {
49 set_bit(bitmap, inr - 1);
50 tfs_bwrite(sbi, sbi->s_inode_bitmap, bitmap);
53 free(bitmap);
54 return inr;