-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
290 lines (224 loc) · 7 KB
/
FileSystem.cpp
File metadata and controls
290 lines (224 loc) · 7 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Project Untitled
*/
#include "FileSystem.h"
#include "DiskInode.h"
#include "Context.h"
/**
* FileSystem implementation
*/
extern Context cxt;
FileSystem::FileSystem() {
}
FileSystem::~FileSystem(){
}
/**
* @return void
* @comment 初始化
*/
void FileSystem::Initialize() {
return;
}
/**
* @return void
* @comment 加载SuperBlock到内存当中
*/
void FileSystem::LoadSuperBlock() {
SuperBlock* g_spb = cxt.GetSuperBlock();
//SuperBlock* g_spb = new SuperBlock();
FILE* MyDisk = cxt.GetMyDisk();
void* p = (void*)g_spb; //指向g_spb的void指针
fseek(MyDisk, 0, SEEK_SET); //定位到MyDisk开头位置
if (!(fread(p, Inode::BLOCK_SIZE * 2, 1, MyDisk))) //从My读取两个BLOCK_SIZE大小的数据
{
cout << "LoadSuperBlock失败" << endl;
return;
}
this->f_SuperBlock = g_spb;
g_spb->s_flock = 0;
g_spb->s_ilock = 0;
return;
}
/**
* @return SuperBlock*
* @comment 返回当前的SuperBlock
*/
SuperBlock* FileSystem::GetFS() {
return this->f_SuperBlock;
}
/**
* @return void
* @comment 更新SuperBlock到硬盘当中
*/
void FileSystem::Update() {
SuperBlock* g_spb = this->f_SuperBlock;
FILE* MyDisk = cxt.GetMyDisk();
void* p = (void*)g_spb; //指向g_spb的void指针
fseek(MyDisk, 0, SEEK_SET); //定位到MyDisk开头位置
if (!(fwrite(p, Inode::BLOCK_SIZE * 2, 1, MyDisk))) //从My读取两个BLOCK_SIZE大小数据
{
cout << "Update失败" << endl;
return;
}
return;
}
/**
* @return Inode*
* @comment 分配Inode结点
*/
Inode* FileSystem::IAlloc() {
Inode* pNode = new Inode();
int ino;
SuperBlock* sb = this->f_SuperBlock;
FILE* MyDisk = cxt.GetMyDisk();
if (sb->s_ninode == 0)
{
int ino = -1;
fseek(MyDisk, FileSystem::INODE_ZONE_START_SECTOR*Inode::BLOCK_SIZE, SEEK_SET); //寻址到Inode区的开始位置
unsigned int d_mode;
void* p = (void*)&d_mode;
int inc = (Inode::BLOCK_SIZE / FileSystem::INODE_NUMBER_PER_SECTOR) - sizeof(unsigned int); //两个相邻Inode结点的d_mode的间隔减去unsigned int 的大小
for (int i = 0; i < FileSystem::INODE_ZONE_SIZE; i++) //读取每个Inode的d_mode的数据
{
if (!(fread(p, 1 * sizeof(unsigned int), 1, MyDisk)))
{
cout << "读取第"<<i<<"位置的d_mode失败" << endl;
return NULL;
}
ino++;
if (d_mode != 0) //该Inode已经被占用
{
continue;
}
sb->s_inode[sb->s_ninode++] = ino; //将空闲的Inode存入Inode索引表当中
fseek(MyDisk, inc, SEEK_CUR);
if (sb->s_ninode >= 100) //说明读取的Inode已经填满Inode索引表
{
break;
}
}
if (sb->s_ninode <= 0)
{
cout << "磁盘已经没有空闲的Inode" << endl;
return NULL;
}
}
while (sb->s_ninode) //因为在IFree过程当中可能会释放已经存在于Inode索引表的表项(当错误地使用IFree时),可能会造成同一个Inode多次分配,所以要循环查找来找到合法的空闲Inode。
{
ino = sb->s_inode[--sb->s_ninode]; //取sb->s_inode栈顶的元素作为分配的Inode
fseek(MyDisk, FileSystem::INODE_ZONE_START_SECTOR*Inode::BLOCK_SIZE + (Inode::BLOCK_SIZE / FileSystem::INODE_NUMBER_PER_SECTOR)*ino, SEEK_SET); //寻址到ino的开始位置
DiskInode DInode;
void* p = (void*)&DInode;
if (!(fread(p, 1 * sizeof(DiskInode), 1, MyDisk)))
{
cout << "读取第" << ino << "位置的DiskInode失败" << endl;
return NULL;
}
pNode->FromDiskInode(DInode);
pNode->i_number = ino;
if (pNode->i_mode == 0)
{
pNode->Clean();
this->Update(); //更新SuperBlock
return pNode;
}
}
return NULL;
}
/**
* @param number
* @return void
* @comment 释放Inode结点
*/
void FileSystem::IFree(int number) {
SuperBlock* sb = this->f_SuperBlock;
FILE* MyDisk = cxt.GetMyDisk();
if (number < 0 || number >= FileSystem::INODE_NUMBER_PER_SECTOR*FileSystem::INODE_ZONE_SIZE) //判断number是否合法
{
return;
}
unsigned int d_mode = 0;
void* p = (void*)&d_mode;
fseek(MyDisk, FileSystem::INODE_ZONE_START_SECTOR*Inode::BLOCK_SIZE + (Inode::BLOCK_SIZE / FileSystem::INODE_NUMBER_PER_SECTOR)*number, SEEK_SET); //寻址到number的开始位置
if (!(fwrite(p, 1 * sizeof(unsigned int), 1, MyDisk)))
{
cout << "写第" << number << "位置的d_mode失败" << endl;
return;
}
if (sb->s_ninode >= 100)
{
return;
}
sb->s_inode[sb->s_ninode++] = number;
this->Update(); //更新SuperBlock
return;
}
/**
* @return int
* @comment 分配DIskBlock
*/
int FileSystem::Alloc() {
int blkno;
SuperBlock* sb = this->f_SuperBlock;
FILE* MyDisk = cxt.GetMyDisk();
blkno = sb->s_free[--sb->s_nfree]; //从空闲块索引表当中读取blkno
if (blkno == 0) //说明已经没有空闲的盘块了
{
sb->s_nfree = 1;
cout << "Alloc失败,已经没有空闲的盘块了" << endl;
return 0;
}
if (sb->s_nfree <= 0) //已经没有直接管理的盘块了,需要将间接索引的盘块读取进来
{
int addr = (FileSystem::DATA_ZONE_START_SECTOR + (blkno - 1))*Inode::BLOCK_SIZE; //计算第blkno个盘块的起始位置
fseek(MyDisk, addr, SEEK_SET);
void* p = (void*)&sb->s_nfree;
if (!(fread(p, 1 * sizeof(int), 1, MyDisk))) //读取第blkno个盘块的s_nfree
{
cout << "sb->s_nfree读取失败" << endl;
return 0;
}
p = (void*)sb->s_free; //指向New_spb->s_free的void指针
if (!(fread(p, 100 * sizeof(int), 1, MyDisk))) //读取第blkno个盘块的s_free数组
{
cout << "sb->s_free数组读取失败" << endl;
return 0;
}
}
this->Update(); //更新SuperBlock
return blkno;
}
/**
* @param blkno
* @return void
* @comment 释放DiskBlock
*/
void FileSystem::Free(int blkno) {
SuperBlock* sb = this->f_SuperBlock;
FILE* MyDisk = cxt.GetMyDisk();
if (sb->s_nfree <= 0) //如果当前已经没有空闲的盘块了
{
sb->s_nfree = 1;
sb->s_free[0] = 0;
}
if (sb->s_nfree >= 100) //直接索引盘块已经超过100,需要将当前的索引表的内容写入到间接索引表当中
{
int addr = (FileSystem::DATA_ZONE_START_SECTOR + (blkno - 1))*Inode::BLOCK_SIZE; //计算第blkno个盘块的起始位置
fseek(MyDisk, addr, SEEK_SET);
void* p = (void*)&sb->s_nfree;
if (!(fwrite(p, 1 * sizeof(int), 1, MyDisk))) //写入第blkno个盘块的s_nfree
{
cout << "sb->s_nfree写入失败" << endl;
return;
}
p = (void*)sb->s_free; //指向New_spb->s_free的void指针
if (!(fwrite(p, 100 * sizeof(int), 1, MyDisk))) //写入第blkno个盘块的s_free数组
{
cout << "sb->s_free数组写入失败" << endl;
return;
}
}
sb->s_free[sb->s_nfree++] = blkno; //将blkno放置到空闲盘块索引表的栈顶当中
this->Update(); //更新SuperBlock
return;
}