-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.c
executable file
·357 lines (302 loc) · 6.3 KB
/
fs.c
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
#include "fs.h"
#include "disk.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <math.h>
#define FS_MAGIC 0xf0f03410
#define INODES_PER_BLOCK 128
#define POINTERS_PER_INODE 6
struct fs_superblock {
int magic;
int nblocks;
int ninodeblocks;
int ninodes;
};
struct fs_inode {
int isvalid;
int size;
int direct[POINTERS_PER_INODE];
};
union fs_block {
struct fs_superblock super;
struct fs_inode inode[INODES_PER_BLOCK];
char data[DISK_BLOCK_SIZE];
};
int sup = 0;
int *map;
int fs_format()
{
if(sup != 0) {
printf("Format is not possible, disk already mounted\n");
return 0;
}
else {
union fs_block block;
disk_read(0,block.data);
block.super.magic = FS_MAGIC;
block.super.nblocks = disk_size();
block.super.ninodeblocks = ceil(disk_size()*0.1);
block.super.ninodes = block.super.ninodeblocks*(DISK_BLOCK_SIZE/32);
int i, j, l;
int ninodeblocks = block.super.ninodeblocks;
for(i = 1; i<=ninodeblocks;i++){
disk_read(i,block.data);
for(j=0;j<INODES_PER_BLOCK;j++){
block.inode[j].isvalid = 0;
block.inode[j].size = 0;
for(l=0;l<POINTERS_PER_INODE;l++){
block.inode[j].direct[l] = 0;
}
}
disk_write( i,block.data);
}
return 1;
}
}
void fs_debug()
{
union fs_block block;
disk_read(0,block.data);
if(sup != 0){
printf("superblock:\n");
printf("magic number is valid\n");
printf(" %d blocks\n",block.super.nblocks);
printf(" %d inode blocks\n",block.super.ninodeblocks);
printf(" %d inodes\n",block.super.ninodes);
int i, j, l;
int ninodeblocks = block.super.ninodeblocks;
for(i = 1; i <= ninodeblocks; i++){
disk_read(i,block.data);
for(j=0;j<INODES_PER_BLOCK;j++){
if(block.inode[j].isvalid == 1){
printf("inode %d\n",j );
printf(" size: %d bytes\n",block.inode[j].size );
for(l=0;l<POINTERS_PER_INODE;l++){
if(block.inode[j].direct[l] != 0) {
printf(" blocks: \n");
printf(" %d\n",block.inode[j].direct[l] );
}
}
}
}
}
}
else
printf("Disk not mounted\n");
}
int fs_mount()
{
if(sup != 0){
printf("Disk is already mounted\n");
return 0;
}
else {
union fs_block block;
disk_read(0,block.data);
if(block.super.magic != FS_MAGIC) {
printf("The file system is not valid\n");
return 0;
}
else if(block.super.nblocks == disk_size()) {
block.super.magic = FS_MAGIC;
sup = 1;
map = malloc((block.super.ninodes*6)*sizeof(int));
int u;
for(u =0; u<(block.super.ninodes*6);u++){
map[u] = 0;
}
int i, j, l;
int ninodeblocks = block.super.ninodeblocks;
for(i = 1; i<=ninodeblocks;i++){
disk_read(i,block.data);
for(j=0;j<INODES_PER_BLOCK;j++){
if(block.inode[j].isvalid == 1){
for(l=0;l<POINTERS_PER_INODE;l++){
if(block.inode[j].direct[l] != 0)
map[block.inode[j].direct[l]] = 1;
}
}
}
}
}
else {
printf("Nblocks not valid\n");
return 0;
}
}
return 1;
}
int fs_create()
{
if(sup == 0){
printf("Disk not mounted\n");
return -1;
}
else{
union fs_block block;
disk_read(0,block.data);
int i, j, l;
int ninodeblocks = block.super.ninodeblocks;
for(i = 1; i<=ninodeblocks;i++){
disk_read(i,block.data);
for(j=0;j<INODES_PER_BLOCK;j++){
if(block.inode[j].isvalid == 0){
block.inode[j].isvalid = 1;
for(l=0;l<POINTERS_PER_INODE;l++){
block.inode[j].direct[l] = 0;
}
map[j] = 1;
disk_write( i,block.data);
return j;
}
}
}
}
return -1;
}
int inodeToBlock (int inode) {
union fs_block block;
disk_read(0,block.data);
if(inode > block.super.ninodes)
return -1;
else
return inode/INODES_PER_BLOCK+1;
}
int inodeToOffset(int inode) {
union fs_block block;
disk_read(0,block.data);
if(inode > block.super.ninodes)
return -1;
else
return inode % INODES_PER_BLOCK;
}
int inode_load(int number, struct fs_inode *inode) {
union fs_block b;
int block = inodeToBlock(number);
if(block<0){
return -1;
}
else{
disk_read(block, b.data);
int entry = inodeToOffset(number);
if(entry<0) {
return -1;
}
*inode = b.inode[entry];
}
return 0;
}
int inode_save(int number, struct fs_inode *inode) {
union fs_block b;
int block = inodeToBlock(number);
if(block<0){
return -1;
}
else{
disk_read(block, b.data);
int entry = inodeToOffset(number);
if(entry<0) {
return -1;
}
disk_read(block, b.data);
b.inode[entry] = *inode;
disk_write(block,b.data);
}
return 0;
}
int fs_delete( int number )
{
if(sup !=0){
struct fs_inode d;
if(inode_load(number, &d)<0) {
return 0;
}
else {
if(d.isvalid == 0) {
printf("Inode não valido\n");
return 0;
}
else{
d.isvalid = 0;
inode_save(number, &d);
int i = 0;
while((i< INODES_PER_BLOCK) && (d.direct[i] !=0)) {
map[d.direct[i]] = 1;
i++;
}
return 1;
}
}
}
else
printf("Disk not mounted\n"); return 0;
}
int fs_getsize( int inumber )
{
if(sup !=0){
struct fs_inode d;
if(inode_load(inumber, &d)<0) {
return -1;
}
else
return d.size;
}
else
printf("Disk not mounted\n"); return -1;
}
void copy(char *a, char*b, int c){
int i;
for(i = 0;i<c;i++){
b[i]=a[i];
}
}
int fs_read( int inumber, char *data, int length, int offset )
{
if(sup != 0) {
struct fs_inode f;
inode_load(inumber,&f);
if(f.isvalid == 0) {
printf("Inode não valido\n");
return -1;
}
else {
int read = length;
if(f.size <offset) {
return -1;
}
else if(offset + length > f.size) {
read = f.size - offset;
}
char block[DISK_BLOCK_SIZE];
int n = 0;
int currentOffset = offset;
char *secAdress;
char *dstAdress;
int offsetIntoBlock;
int nBytesCopy;
while (n>read) {
disk_read(f.direct[currentOffset/DISK_BLOCK_SIZE],block);
offsetIntoBlock = currentOffset % DISK_BLOCK_SIZE;
secAdress = block + currentOffset;
dstAdress = data + currentOffset;
nBytesCopy = DISK_BLOCK_SIZE - offsetIntoBlock;
if(nBytesCopy>read) {
nBytesCopy = read;
}
copy(secAdress, dstAdress, nBytesCopy);
read -= nBytesCopy;
currentOffset += nBytesCopy;
}
return 1;
}
}
else
printf("Disk not mounted\n"); return -1;
}
int fs_write( int inumber, const char *data, int length, int offset )
{
return 0;
}