Two fixes
[tfsprogs.git] / mktfs.c
blobeb9e692613730f43f7197a91e37acdd2e5be009f
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"
11 #include "file.h"
12 #include "dirent.h"
14 static void mark_used_blocks(struct tfs_sb_info *sbi)
16 int i = 0;
17 char *buf = malloc(sbi->s_block_size);
19 memset(buf, 0, sbi->s_block_size);
20 for (; i < sbi->s_data_area; i++)
21 set_bit(buf, i);
22 tfs_bwrite(sbi, sbi->s_block_bitmap, buf);
25 static int root_init(struct tfs_sb_info *sbi)
27 int res = 0;
28 int dirty = 0;
29 struct inode *root = tfs_root_init(sbi);
31 if (!root) {
32 printf("ERROR: failed to get root inode!\n");
33 res = -1;
34 goto out_nofree;
37 res = tfs_add_entry(sbi, root, ".", root->i_ino, &dirty);
38 if (res == -1) {
39 TFS_DEBUG("trying to add '.' under %s failed!\n", "/");
40 goto out;
43 res = tfs_add_entry(sbi, root, "..", root->i_ino, &dirty);
44 if (res == -1) {
45 TFS_DEBUG("trying to add .. under %s failed!\n", "/");
46 goto out;
49 if (dirty)
50 tfs_iwrite(sbi, root);
52 out:
53 free_inode(root);
54 out_nofree:
55 return res;
58 void tfs_mkfs(char *image, uint32_t offset)
60 struct tfs_super_block sb;
61 struct tfs_sb_info *sbi;
62 int block_size;
63 int block_bitmap_count, inode_bitmap_count, inode_table_count;
64 uint32_t fs_size;
66 fs_size = open_fs(image);
68 sbi = malloc(sizeof(*sbi));
69 if (!sbi) {
70 printf("malloc for sbi failed!\n");
71 return;
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);
105 cache_init(sbi);
106 root_init(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);
116 int offset = 0;
117 char cmd[64];
118 struct tfs_sb_info *sbi;
120 #if 0
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);
128 #endif
130 if (argc < 3) {
131 printf("Usage: mktfs image offset\n");
132 return 1;
135 tfs_mkfs(argv[1], offset);
137 return 0;