13 /* From ext2fsprogs */
16 static void mark_used_blocks(struct tfs_sb_info
*sbi
)
19 char *buf
= malloc(sbi
->s_block_size
);
21 memset(buf
, 0, sbi
->s_block_size
);
22 for (; i
< sbi
->s_data_area
; i
++)
24 tfs_bwrite(sbi
, sbi
->s_block_bitmap
, buf
);
27 static int root_init(struct tfs_sb_info
*sbi
)
31 struct inode
*root
= tfs_root_init(sbi
);
34 printf("ERROR: failed to get root inode!\n");
39 res
= tfs_add_entry(sbi
, root
, ".", root
->i_ino
, &dirty
);
41 TFS_DEBUG("trying to add '.' under %s failed!\n", "/");
45 res
= tfs_add_entry(sbi
, root
, "..", root
->i_ino
, &dirty
);
47 TFS_DEBUG("trying to add .. under %s failed!\n", "/");
52 tfs_iwrite(sbi
, root
);
60 struct tfs_sb_info
* tfs_mkfs(char *image
, uint32_t offset
)
62 struct tfs_super_block sb
;
63 struct tfs_sb_info
*sbi
;
65 int block_bitmap_count
, inode_bitmap_count
, inode_table_count
;
68 fs_size
= open_fs(image
);
70 sbi
= malloc(sizeof(*sbi
));
72 printf("malloc for sbi failed!\n");
76 memset(&sb
, 0, sizeof sb
);
77 memset(sbi
, 0, sizeof(*sbi
));
78 sb
.s_magic
= TFS_MAGIC
;
79 sb
.s_block_shift
= sbi
->s_block_shift
= 9; /* 512 Bytes */
80 sbi
->s_block_size
= block_size
= 1 << sb
.s_block_shift
;
81 sb
.s_blocks_count
= sbi
->s_blocks_count
= fs_size
/ block_size
;
84 /* A half of blocks count */
85 sb
.s_inodes_count
= sbi
->s_inodes_count
= sb
.s_blocks_count
/ 2;
87 sbi
->s_block_bitmap_count
= block_bitmap_count
= roundup(sb
.s_blocks_count
, (block_size
* 8));
88 sbi
->s_inode_bitmap_count
= inode_bitmap_count
= roundup(sb
.s_inodes_count
, (block_size
* 8));
89 sbi
->s_inode_table_count
= inode_table_count
= sb
.s_inodes_count
* sizeof(struct tfs_inode
) / block_size
;
91 sb
.s_inode_bitmap
= sbi
->s_inode_bitmap
= 0;
92 sb
.s_block_bitmap
= sbi
->s_block_bitmap
= inode_bitmap_count
;
93 sb
.s_inode_table
= sbi
->s_inode_table
= sb
.s_block_bitmap
+ block_bitmap_count
;
94 sb
.s_data_area
= sbi
->s_data_area
= sb
.s_inode_table
+ inode_table_count
;
96 /* Skip the boot sector and super block sector */
97 sb
.s_offset
= sbi
->s_offset
= offset
+ 2;
99 sb
.s_free_inodes_count
= sbi
->s_free_inodes_count
= sb
.s_inodes_count
;
100 sb
.s_free_blocks_count
= sbi
->s_free_blocks_count
= sb
.s_blocks_count
- sb
.s_data_area
;
102 sbi
->s_inodes_per_block
= sbi
->s_block_size
/ sizeof(struct tfs_inode
);
105 mark_used_blocks(sbi
);
110 /* Write the suber block back to image */
111 write_sector(sbi
->s_offset
- 1, &sb
, 1);
117 void tfs_cp(struct tfs_sb_info
*sbi
, char *filename
)
119 char *file
= malloc(strlen(filename
) + 2);
127 strcat(file
, filename
);
129 fobj
= tfs_open(sbi
, file
);
131 printf("tfs_open: open file %s error!\n", filename
);
135 fd
= open("filename", O_RDONLY
);
137 printf("open file %s error!\n", filename
);
140 while ((count
= read(fd
, buf
, sizeof(buf
))) > 0) {
141 bytes_written
= tfs_write(fobj
, buf
, count
);
142 if (bytes_written
== -1)
143 printf("write error!\n");
145 printf("== %d bytes written!\n", bytes_written
);
152 int main(int argc
, char *argv
[])
154 int size
= sizeof(struct tfs_inode
);
158 struct tfs_sb_info
*sbi
;
161 printf("inode size: %d(0x%x)\n", size
, size
);
163 size
= sizeof(struct tfs_dir_entry
);
164 printf("dentry size: %d(0x%x)\n", size
, size
);
166 size
= sizeof(struct tfs_super_block
);
167 printf("super block size: %d(0x%0x)\n", size
, size
);
171 printf("Usage: mktfs image offset files\n");
173 } else if (argc
== 3) {
174 offset
= atoi(argv
[2]);
179 sbi
= tfs_mkfs(argv
[1], offset
);
181 printf("mkfs failed!\n");
185 for (i
= 0; i
< count
; i
++) {
186 printf("copying file %s... \n", files
[i
]);
187 tfs_cp(sbi
, files
[i
]);