implememnt balloc stuff
[tfsprogs.git] / mktfs.c
blob99880dd97f46541362b376d9c232712fcd8b1f36
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <malloc.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <sys/stat.h>
10 #include "tfs.h"
12 /* From ext2fsprogs */
15 static void mark_used_blocks(struct tfs_sb_info *sbi)
17 int i = 0;
18 char *buf = malloc(sbi->s_block_size);
20 memset(buf, 0, sbi->s_block_size);
21 for (; i < sbi->s_data_area; i++)
22 set_bit(buf, i);
23 tfs_bwrite(sbi, sbi->s_block_bitmap, buf);
26 #define roundup(x, y) ((x) / (y) + (((x) % (y)) ? 1 : 0))
27 void tfs_mkfs(char *image, uint32_t offset)
29 struct tfs_super_block sb;
30 struct tfs_sb_info sbi;
31 int block_size;
32 int block_bitmap_count, inode_bitmap_count, inode_table_count;
33 uint32_t fs_size;
35 fs_size = open_fs(image);
37 memset(&sb, 0, sizeof sb);
38 sb.s_magic = TFS_MAGIC;
39 sb.s_block_shift = sbi.s_block_shift = 9; /* 512 Bytes */
40 sbi.s_block_size = block_size = 1 << sb.s_block_shift;
41 sb.s_blocks_count = sbi.s_blocks_count = fs_size / block_size;
44 /* A half of blocks count */
45 sb.s_inodes_count = sbi.s_inodes_count = sb.s_blocks_count / 2;
47 sbi.s_block_bitmap_count = block_bitmap_count = roundup(sb.s_blocks_count, (block_size * 8));
48 sbi.s_inode_bitmap_count = inode_bitmap_count = roundup(sb.s_inodes_count, (block_size * 8));
49 sbi.s_inode_table_count = inode_table_count = sb.s_inodes_count * sizeof(struct tfs_inode) / block_size;
51 sb.s_inode_bitmap = sbi.s_inode_bitmap = 0;
52 sb.s_block_bitmap = sbi.s_block_bitmap = inode_bitmap_count;
53 sb.s_inode_table = sbi.s_inode_table = sb.s_block_bitmap + block_bitmap_count;
54 sb.s_data_area = sbi.s_data_area = sb.s_inode_table + inode_table_count;
56 /* Skip the boot sector and super block sector */
57 sb.s_offset = sbi.s_offset = offset + 2;
59 sb.s_free_inodes_count = sbi.s_free_inodes_count = sb.s_inodes_count;
60 sb.s_free_blocks_count = sbi.s_free_blocks_count = sb.s_blocks_count - sb.s_data_area;
63 mark_used_blocks(&sbi);
65 /* Write the suber block back to image */
66 write_sector(sbi.s_offset - 1, &sb, 1);
70 int main(int argc, char *argv[])
72 int size = sizeof(struct tfs_inode);
73 int offset = 0;
75 #if 1
76 printf("inode size: %d(0x%x)\n", size, size);
78 size = sizeof(struct tfs_dir_entry);
79 printf("dentry size: %d(0x%x)\n", size, size);
81 size = sizeof(struct tfs_super_block);
82 printf("super block size: %d(0x%0x)\n", size, size);
83 #endif
85 if (argc < 2) {
86 printf("Usage: mktfs image offset\n");
87 return 1;
88 } else if (argc == 3) {
89 offset = atoi(argv[2]);
92 tfs_mkfs(argv[1], offset);
94 return 0;