Add the implementation of tfs
[thunix.git] / fs / tfs / ialloc.c
blob42f5211376081cbcee76854da9d6da161a2f30c4
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;
57 #if 0 /* the deubg part */
58 #include <stdlib.h>
59 int main(int argc, char *argv[])
61 int i;
62 int inr;
63 struct tfs_sb_info *sbi;
64 char *fs = argv[1];
65 char *command = argv[2];
66 char **inodes = argv + 3;
67 int count = argc - 3;
69 if (argc < 4) {
70 printk("Usage: ialloc tfs.img command inodes...\n");
71 printk(" alloc, to alloc inodes\n");
72 printk(" feee, to free inodes\n");
73 return 1;
76 sbi = tfs_mount_by_fsname(fs);
77 if (!sbi) {
78 printk("tfs mount failed!\n");
79 return 1;
82 if (strcmp(command, "alloc") == 0) {
83 for (i = 0; i < count; i++) {
84 inr = atoi(inodes[i]);
85 printk("trying to alloc inode %u\n", inr);
86 inr = tfs_alloc_inode(sbi, inr);
87 printk("inode number: %u allocated\n", inr);
89 } else if (strcmp(command, "free") == 0) {
90 for (i = 0; i < count; i++) {
91 inr = atoi(inodes[i]);
92 printk("trying to free inode %u\n", inr);
93 inr = tfs_free_inode(sbi, inr);
94 printk("inode number: %d freed\n", inr);
96 } else {
97 printk("Unknown command!\n");
100 return 0;
102 #endif