-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg.cpp
153 lines (140 loc) · 4.77 KB
/
img.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "img.h"
#include <cassert>
#include <cstring>
#include "InodeManager.h"
#include "util.h"
void ImgMaker::initFloppy(std::shared_ptr<MyDisk> bd) {
auto sbm_ = std::make_shared<SuperBlockManager>(bd);
auto bm_ = std::make_shared<BlockManager>(bd, sbm_, false);
auto im_ = std::make_shared<InodeManager>(sbm_, bm_);
SuperBlock sb;
memset(&sb, 0, sizeof(SuperBlock));
// Initialize superblock
// sb_.s_inodes_count_ = ? ;
sb.s_blocks_count_ = 1440;
// sb_.s_r_blocks_count_ = ;
// sb_.s_free_blocks_count_ = 1439;
// sb_.s_free_inodes_count_ = ;
sb.s_first_data_block_ = 1;
sb.s_log_block_size_ = 0;
// sb_.s_log_frag_size_ = ;
sb.s_blocks_per_group_ = 1439;
// sb_.s_frags_per_group_ = ;
sb.s_inodes_per_group_ = 23 * (1024 / sizeof(inode));
sb.s_mtime_ = time(nullptr);
sb.s_wtime_ = time(nullptr);
// sb_.s_mnt_count_ = ;
// sb_.s_max_mnt_count_ = ;
sb.s_magic_ = EXT2_SUPER_MAGIC;
// TODO 假设这里是挂载
sb.s_state_ = EXT2_ERROR_FS;
sbm_->writeSuperBlock(&sb);
// Initialize block group descriptor table
FSBlock blk{std::make_unique<char[]>(1024)};
auto bgd = reinterpret_cast<Block_Group_Descriptor*>(blk.s_.get());
bgd[0].bg_block_bitmap_ = 3;
bgd[0].bg_inode_bitmap_ = 4;
bgd[0].bg_inode_table_ = 5;
bm_->writeBlock(blk, 2);
bm_->refresh();
// Initialize block bitmap
memset(blk.s_.get(), 0, 1024);
bm_->writeBlock(blk, 3);
for (int i = 1; i < 28; i++) {
// TODO: OPTIMIZE
bm_->tagBlock(i, 1);
}
// Initialize inode bitmap
bm_->writeBlock(blk, 4);
// Initialize inode table
bm_->writeBlock(blk, 5);
inode root_inode;
memset(&root_inode, 0, sizeof(inode));
root_inode.i_mode_ = EXT2_S_IFDIR | 777;
root_inode.i_links_count_ = 2;
//
auto iid = im_->new_inode(root_inode);
assert(iid == 1);
im_->dir_add_dentry(1, 1, ".", EXT2_FT_DIR);
im_->dir_add_dentry(1, 1, "..", EXT2_FT_DIR);
}
void ImgMaker::initFloppyPlus(std::shared_ptr<MyDisk> bd) {
auto sbm_ = std::make_shared<SuperBlockManager>(bd);
auto bm_ = std::make_shared<BlockManager>(bd, sbm_, false);
auto im_ = std::make_shared<InodeManager>(sbm_, bm_);
SuperBlock sb;
memset(&sb, 0, sizeof(SuperBlock));
// Initialize superblock
// sb_.s_inodes_count_ = ? ;
sb.s_blocks_count_ = 24577;
// sb_.s_r_blocks_count_ = ;
// sb_.s_free_blocks_count_ = 1439;
// sb_.s_free_inodes_count_ = ;
sb.s_first_data_block_ = 1;
sb.s_log_block_size_ = 0;
// sb_.s_log_frag_size_ = ;
sb.s_blocks_per_group_ = 8192;
// sb_.s_frags_per_group_ = ;
sb.s_inodes_per_group_ = 214 * (1024 / sizeof(inode));
sb.s_mtime_ = time(nullptr);
sb.s_wtime_ = time(nullptr);
// sb_.s_mnt_count_ = ;
// sb_.s_max_mnt_count_ = ;
sb.s_magic_ = EXT2_SUPER_MAGIC;
// TODO 假设这里是挂载
sb.s_state_ = EXT2_ERROR_FS;
sbm_->writeSuperBlock(&sb);
// Initialize block group descriptor table
FSBlock blk{std::make_unique<char[]>(1024)};
auto bgd = reinterpret_cast<Block_Group_Descriptor*>(blk.s_.get());
bgd[0].bg_block_bitmap_ = 3;
bgd[0].bg_inode_bitmap_ = 4;
bgd[0].bg_inode_table_ = 5;
bgd[1].bg_block_bitmap_ = 8195;
bgd[1].bg_inode_bitmap_ = 8196;
bgd[1].bg_inode_table_ = 8197;
bgd[2].bg_block_bitmap_ = 16385;
bgd[2].bg_inode_bitmap_ = 16386;
bgd[2].bg_inode_table_ = 16387;
bm_->writeBlock(blk, 2);
bm_->refresh();
// Initialize block bitmap
// Initialize inode bitmap
// Initialize inode table
memset(blk.s_.get(), 0, 1024);
for (int group = 0; group < bm_->bgd_.size(); group++) {
bm_->writeBlock(blk, bm_->bgd_[group].bg_block_bitmap_);
bm_->writeBlock(blk, bm_->bgd_[group].bg_inode_bitmap_);
// TODO: OPTIMIZE
// TODO: ASSERT 1 block bitmap ,inode tbl place just before data block
for (int i = 0; i < 214; i++) {
bm_->writeBlock(blk, bm_->bgd_[group].bg_inode_table_ + i);
}
}
// dont forget tag superblock and bgd
for (int group = 0; group < bm_->bgd_.size(); group++) {
if (group == 0 || group == 1 || isPowerOf(group, 3) ||
isPowerOf(group, 5) || isPowerOf(group, 7)) {
size_t sb_bid = sb.s_first_data_block_ + group * sb.s_blocks_per_group_;
bm_->tagBlock(sb_bid, 1);
bm_->tagBlock(sb_bid + 1, 1);
} else {
size_t bgd_bid = sb.s_first_data_block_ + group * sb.s_blocks_per_group_;
bm_->tagBlock(bgd_bid, 1);
}
bm_->tagBlock(bm_->bgd_[group].bg_block_bitmap_, 1);
bm_->tagBlock(bm_->bgd_[group].bg_inode_bitmap_, 1);
for (int i = 0; i < 214; i++) {
bm_->tagBlock(bm_->bgd_[group].bg_inode_table_ + i, 1);
}
}
inode root_inode;
memset(&root_inode, 0, sizeof(inode));
root_inode.i_mode_ = EXT2_S_IFDIR | 777;
root_inode.i_links_count_ = 2;
//
auto iid = im_->new_inode(root_inode);
assert(iid == 1);
im_->dir_add_dentry(1, 1, ".", EXT2_FT_DIR);
im_->dir_add_dentry(1, 1, "..", EXT2_FT_DIR);
}