14 static void mark_used_blocks(struct tfs_sb_info
*sbi
)
17 char *buf
= malloc(sbi
->s_block_size
);
19 memset(buf
, 0, sbi
->s_block_size
);
20 for (; i
< sbi
->s_data_area
; i
++)
22 tfs_bwrite(sbi
, sbi
->s_block_bitmap
, buf
);
25 static int root_init(struct tfs_sb_info
*sbi
)
29 struct inode
*root
= tfs_root_init(sbi
);
32 printf("ERROR: failed to get root inode!\n");
37 res
= tfs_add_entry(sbi
, root
, ".", root
->i_ino
, &dirty
);
39 TFS_DEBUG("trying to add '.' under %s failed!\n", "/");
43 res
= tfs_add_entry(sbi
, root
, "..", root
->i_ino
, &dirty
);
45 TFS_DEBUG("trying to add .. under %s failed!\n", "/");
50 tfs_iwrite(sbi
, root
);
58 void tfs_mkfs(char *image
, uint32_t offset
)
60 struct tfs_super_block sb
;
61 struct tfs_sb_info
*sbi
;
63 int block_bitmap_count
, inode_bitmap_count
, inode_table_count
;
66 fs_size
= open_fs(image
);
68 sbi
= malloc(sizeof(*sbi
));
70 printf("malloc for sbi failed!\n");
74 memset(&sb
, 0, sizeof sb
);
75 memset(sbi
, 0, sizeof(*sbi
));
76 sb
.s_magic
= TFS_MAGIC
;
77 sb
.s_block_shift
= sbi
->s_block_shift
= 10; /* 1K */
78 sbi
->s_block_size
= block_size
= 1 << sb
.s_block_shift
;
79 sb
.s_blocks_count
= sbi
->s_blocks_count
= fs_size
/ block_size
;
82 /* A half of blocks count */
83 sb
.s_inodes_count
= sbi
->s_inodes_count
= sb
.s_blocks_count
/ 2;
85 sbi
->s_block_bitmap_count
= block_bitmap_count
= roundup(sb
.s_blocks_count
, (block_size
* 8));
86 sbi
->s_inode_bitmap_count
= inode_bitmap_count
= roundup(sb
.s_inodes_count
, (block_size
* 8));
87 sbi
->s_inode_table_count
= inode_table_count
= sb
.s_inodes_count
* sizeof(struct tfs_inode
) / block_size
;
89 sb
.s_inode_bitmap
= sbi
->s_inode_bitmap
= 0;
90 sb
.s_block_bitmap
= sbi
->s_block_bitmap
= inode_bitmap_count
;
91 sb
.s_inode_table
= sbi
->s_inode_table
= sb
.s_block_bitmap
+ block_bitmap_count
;
92 sb
.s_data_area
= sbi
->s_data_area
= sb
.s_inode_table
+ inode_table_count
;
94 /* Skip the boot sector and super block sector */
95 sb
.s_offset
= sbi
->s_offset
= offset
+ 2;
97 sb
.s_free_inodes_count
= sbi
->s_free_inodes_count
= sb
.s_inodes_count
;
98 sb
.s_free_blocks_count
= sbi
->s_free_blocks_count
= sb
.s_blocks_count
- sb
.s_data_area
;
100 sbi
->s_inodes_per_block
= sbi
->s_block_size
/ sizeof(struct tfs_inode
);
103 mark_used_blocks(sbi
);
108 /* Write the suber block back to image */
109 write_sector(sbi
->s_offset
- 1, &sb
, 1);
113 int main(int argc
, char *argv
[])
115 int size
= sizeof(struct tfs_inode
);
118 struct tfs_sb_info
*sbi
;
121 printf("inode size: %d(0x%x)\n", size
, size
);
123 size
= sizeof(struct tfs_dir_entry
);
124 printf("dentry size: %d(0x%x)\n", size
, size
);
126 size
= sizeof(struct tfs_super_block
);
127 printf("super block size: %d(0x%0x)\n", size
, size
);
131 printf("Usage: mktfs image offset\n");
135 tfs_mkfs(argv
[1], offset
);