-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInode.cpp
More file actions
398 lines (328 loc) · 10 KB
/
Inode.cpp
File metadata and controls
398 lines (328 loc) · 10 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* Project Untitled
*/
#include "Inode.h"
#include "Context.h"
/**
* Inode implementation
*/
extern Context cxt;
Inode::Inode() {
this->i_mode = 0;
this->i_number = -1;
this->i_uid = -1;
this->i_gid = -1;
this->i_size = 0;
for (int i = 0; i < 10; i++)
{
this->i_addr[i] = 0;
}
}
Inode::~Inode() {
}
/**
* @return void
* @comment 对当前Inode进行读入,读入的IO参数在Context当中
*/
void Inode::ReadI() {
int lbn; //文件的逻辑块号
int bn; //lbn对应的物理块号
int offset; //当前读入的偏移量
int nbytes; //读入的字节数
void* Buffer = (void*)cxt.c_ReadBuffer; //读入的数据缓冲区
if (!Buffer)
{
cout << "缓存区无效" << endl;
return;
}
FILE* MyDisk = cxt.GetMyDisk();
if (cxt.GetIOParameter()->m_Count <= 0)
{
return;
}
while (cxt.GetIOParameter()->m_Count > 0)
{
lbn = cxt.GetIOParameter()->m_Offset / Inode::BLOCK_SIZE; //计算逻辑块号
offset= cxt.GetIOParameter()->m_Offset % Inode::BLOCK_SIZE; //计算块内偏移量
nbytes = Inode::BLOCK_SIZE - offset; //nbytes有2种情况:
//1.读入的字节数没有超过当前块的末尾
//2.读入的字节数横跨至少一个盘块的边界
if (nbytes > cxt.GetIOParameter()->m_Count)
nbytes = cxt.GetIOParameter()->m_Count;
int remain = this->i_size - cxt.GetIOParameter()->m_Offset;
if (nbytes > remain)
nbytes = remain;
if ((bn = this->Bmap(lbn)) == 0) //获取逻辑盘块lbn对应的物理盘块bn
{
return;
}
int addr = (FileSystem::DATA_ZONE_START_SECTOR + (bn - 1))*Inode::BLOCK_SIZE + offset; //计算第blkno个盘块的起始位置
fseek(MyDisk, addr, SEEK_SET);
if (!(fread(Buffer, nbytes, 1, MyDisk))) //读取第blkno个盘块的s_nfree
{
cout <<"第"<<bn<< "块盘块读取失败" << endl;
return;
}
Buffer = (void*)((char*)Buffer + nbytes); //Buffer指向的缓冲区也加上offset
cxt.GetIOParameter()->m_Base += nbytes;
cxt.GetIOParameter()->m_Offset += nbytes;
cxt.GetIOParameter()->m_Count -= nbytes;
}
return;
}
/**
* @return void
* @comment 对当前Inode进行写入,写入的IO参数在Context当中
*/
void Inode::WriteI() {
int lbn; //文件的逻辑块号
int bn; //lbn对应的物理块号
int offset; //当前读入的偏移量
int nbytes; //读入的字节数
void* Buffer = (void*)cxt.c_ReadBuffer; //读入的数据缓冲区
if (!Buffer)
{
cout << "缓存区无效" << endl;
return;
}
FILE* MyDisk = cxt.GetMyDisk();
if (cxt.GetIOParameter()->m_Count <= 0) //没有东西写入
{
return;
}
while (cxt.GetIOParameter()->m_Count > 0)
{
lbn = cxt.GetIOParameter()->m_Offset / Inode::BLOCK_SIZE; //计算逻辑块号
offset = cxt.GetIOParameter()->m_Offset % Inode::BLOCK_SIZE; //计算块内偏移量
nbytes = Inode::BLOCK_SIZE - offset; //nbytes有2种情况:
//1.写入的字节数没有超过当前块的末尾
//2.写入的字节数横跨至少一个盘块的边界
if (nbytes > cxt.GetIOParameter()->m_Count)
nbytes = cxt.GetIOParameter()->m_Count;
if ((bn = this->Bmap(lbn)) == 0) //获取逻辑盘块lbn对应的物理盘块bn,其中包含新的盘块的申请
{
return;
}
int addr = (FileSystem::DATA_ZONE_START_SECTOR + (bn - 1))*Inode::BLOCK_SIZE + offset; //计算第blkno个盘块的起始位置
fseek(MyDisk, addr, SEEK_SET);
if (!(fwrite(Buffer, nbytes, 1, MyDisk))) //读取第blkno个盘块的s_nfree
{
cout << "第" << bn << "块盘块写入失败" << endl;
return;
}
Buffer = (void*)((char*)Buffer + nbytes); //Buffer指向的缓冲区也加上offset
cxt.GetIOParameter()->m_Base += nbytes;
cxt.GetIOParameter()->m_Offset += nbytes;
cxt.GetIOParameter()->m_Count -= nbytes;
if (this->i_size < cxt.GetIOParameter()->m_Offset) //如果写入范围大于文件的大小,则增加文件大小
{
this->i_size = cxt.GetIOParameter()->m_Offset;
}
}
cxt.GetFileSystem()->Update();
this->IUpdate();
return;
}
/**
* @return void
* @comment 清理Inode的参数
*/
void Inode::Clean() {
this->i_mode = 0;
this->i_uid = -1;
this->i_gid = -1;
this->i_size = 0;
for (int i = 0; i < 10; i++)
{
this->i_addr[i] = 0;
}
return;
}
/**
* @return void
* @comment 将包含外存Inode字符块中信息拷贝到内存Inode中
*/
void Inode::ICopy() {
FILE* MyDisk = cxt.GetMyDisk();
int ino = this->i_number;
if (ino < 0 || ino >= FileSystem::INODE_NUMBER_PER_SECTOR*FileSystem::INODE_ZONE_SIZE) //判断ino是否合法
{
cout << "Icopy():结点指向Inode编号不合法" << endl;
return;
}
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;
}
this->FromDiskInode(DInode);
return;
}
/**
* @comment 将包含内存Inode字符块中信息拷贝到外存Inode中
*
*/
void Inode::IUpdate() {
FILE* MyDisk = cxt.GetMyDisk();
int ino = this->i_number;
if (ino < 0 || ino >= FileSystem::INODE_NUMBER_PER_SECTOR*FileSystem::INODE_ZONE_SIZE) //判断ino是否合法
{
cout << "IUpdate():结点指向Inode编号不合法" << endl;
return;
}
DiskInode DInode = this->ToDiskInode();
fseek(MyDisk, FileSystem::INODE_ZONE_START_SECTOR*Inode::BLOCK_SIZE + (Inode::BLOCK_SIZE / FileSystem::INODE_NUMBER_PER_SECTOR)*ino, SEEK_SET); //寻址到ino的开始位置
void* p = (void*)&DInode;
if (!(fwrite(p, 1 * sizeof(DiskInode), 1, MyDisk)))
{
cout << "写入第" << ino << "位置的DiskInode失败" << endl;
return;
}
return;
}
/**
* @param lbn
* @return int
* @comment 将逻辑盘块映射到物理盘块
*/
int Inode::Bmap(int lbn) {
int FirstBlkno;
int SecondBlkno;
FileSystem* fileSys = cxt.GetFileSystem();
FILE* MyDisk = cxt.GetMyDisk();
int phyBlkno; //转换得到的物理盘块号
int iTable[Inode::ADDRESS_PER_INDEX_BLOCK] = {0}; //用于访问索引盘块中一次间接、两次间接索引表
int index; //为物理盘块在直接索引表的表项(用于大型文件和巨型文件当中)
if (lbn < 6) //小型文件,盘块直接由索引表管理
{
phyBlkno = this->i_addr[lbn];
if (phyBlkno == 0 && (FirstBlkno = fileSys->Alloc()) != 0) //如果对应索引表项没有分配盘块号,且能分配到新的盘块
{
this->i_addr[lbn] = FirstBlkno;
phyBlkno = FirstBlkno;
}
return phyBlkno;
}
else
{
if (lbn < Inode::LARGE_FILE_BLOCK) //大型文件,第7、8个表项为一级间接索引
{
index = (lbn - Inode::SMALL_FILE_BLOCK) / Inode::ADDRESS_PER_INDEX_BLOCK + 6;
}
else //巨型文件,第9、10个表项为二级间接索引
{
index = (lbn - Inode::LARGE_FILE_BLOCK) / (Inode::ADDRESS_PER_INDEX_BLOCK * Inode::ADDRESS_PER_INDEX_BLOCK) + 8;
}
phyBlkno = this->i_addr[index];
FirstBlkno = phyBlkno;
if (phyBlkno == 0) //phyBlkno对应表项没有分配
{
if ((FirstBlkno = fileSys->Alloc()) == 0) //申请一个空闲盘块给phyBlkno对应的表项
{
return 0;
}
this->i_addr[index] = FirstBlkno;
}
else
{
int addr = (FileSystem::DATA_ZONE_START_SECTOR + (FirstBlkno - 1))*Inode::BLOCK_SIZE; //计算第phyBlkno个盘块的起始位置
fseek(MyDisk, addr, SEEK_SET);
if (!(fread(iTable, Inode::ADDRESS_PER_INDEX_BLOCK*sizeof(int), 1, MyDisk)))
{
cout << "第" << phyBlkno << "块盘块读入失败" << endl;
return 0;
}
}
if (index >= 8) //巨型文件,当前的iTable还不是直接索引表,需要再读入一次
{
index = ((lbn - Inode::LARGE_FILE_BLOCK) / Inode::ADDRESS_PER_INDEX_BLOCK) % Inode::ADDRESS_PER_INDEX_BLOCK;
phyBlkno = iTable[index];
if (phyBlkno == 0) //二级间接索引表对应的表项没有分配盘块
{
if ((SecondBlkno = fileSys->Alloc()) == 0) //申请一个空闲盘块给phyBlkno对应的表项
{
return 0;
}
iTable[index] = SecondBlkno;
int addr = (FileSystem::DATA_ZONE_START_SECTOR + (FirstBlkno - 1))*Inode::BLOCK_SIZE;
fseek(MyDisk, addr, SEEK_SET);
if (!(fwrite(iTable, Inode::ADDRESS_PER_INDEX_BLOCK * sizeof(int), 1, MyDisk))) //将iTable写入到间接第一级索引表中
{
cout << "第" << SecondBlkno << "块盘块写入失败" << endl;
return 0;
}
for (int i = 0; i < Inode::ADDRESS_PER_INDEX_BLOCK; i++) //这里全置0是因为新申请的表项对应的二级索引表盘块为空
{
iTable[i] = 0;
}
}
else
{
SecondBlkno = phyBlkno;
int addr = (FileSystem::DATA_ZONE_START_SECTOR + (SecondBlkno - 1))*Inode::BLOCK_SIZE; //计算第phyBlkno个盘块的起始位置
fseek(MyDisk, addr, SEEK_SET);
if (!(fread(iTable, Inode::ADDRESS_PER_INDEX_BLOCK * sizeof(int), 1, MyDisk)))
{
cout << "第" << phyBlkno << "块盘块读入失败" << endl;
return 0;
}
}
FirstBlkno = SecondBlkno;
}
if (lbn < Inode::LARGE_FILE_BLOCK)
{
index = (lbn - Inode::SMALL_FILE_BLOCK) % Inode::ADDRESS_PER_INDEX_BLOCK;
}
else
{
index = (lbn - Inode::LARGE_FILE_BLOCK) % Inode::ADDRESS_PER_INDEX_BLOCK;
}
if ((phyBlkno = iTable[index]) == 0 && (SecondBlkno = fileSys->Alloc()) != 0)
{
phyBlkno = SecondBlkno;
iTable[index] = phyBlkno;
int addr = (FileSystem::DATA_ZONE_START_SECTOR + (FirstBlkno - 1))*Inode::BLOCK_SIZE; //计算第phyBlkno个盘块的起始位置
fseek(MyDisk, addr, SEEK_SET);
if (!(fwrite(iTable, Inode::ADDRESS_PER_INDEX_BLOCK * sizeof(int), 1, MyDisk)))
{
cout << "第" << FirstBlkno << "块盘块写入失败" << endl;
return 0;
}
}
return phyBlkno;
}
return 0;
}
/**
* @comment 将DiskInode的值赋给Inode
*
*/
void Inode::FromDiskInode(DiskInode DInode) {
this->i_mode = DInode.d_mode;
this->i_uid = DInode.d_uid;
this->i_gid = DInode.d_gid;
this->i_size = DInode.d_size;
for (int i = 0; i < 10; i++)
{
this->i_addr[i] = DInode.d_addr[i];
}
}
/**
* @comment 将Inode的值赋给DiskInode
*
*/
DiskInode Inode::ToDiskInode() {
DiskInode DInode;
DInode.d_mode = this->i_mode;
DInode.d_uid = this->i_uid;
DInode.d_gid = this->i_gid;
DInode.d_size = this->i_size;
for (int i = 0; i < 10; i++)
{
DInode.d_addr[i] = this->i_addr[i];
}
return DInode;
}