-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufs.c
157 lines (143 loc) · 3.77 KB
/
ufs.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
// Creadit: https://www.youtube.com/watch?v=n2AAhiujAqs&ab_channel=drdelhart
#include "ufs.h"
struct superblock sb;
struct inode *inodes;
struct disk_block *dbs;
int find_empty_inode()
{
int i;
for (i = 0; i < sb.num_inodes; i++)
{
if (inodes[i].first_block == -1)
{
return i;
}
}
return -1;
}
int find_empty_block()
{
int i;
for (i = 0; i < sb.num_blocks; i++)
{
if (dbs[i].next_block_num == -1)
{
return i;
}
}
return -1;
}
int allocate_file(char name[8]){
int in = find_empty_inode();
int block = find_empty_block();
inodes[in].first_block = block;
dbs[block].next_block_num = -2;
strcpy((inodes[in]).name, name);
// return the file descriptor
return in;
}
void shorten_file(int bn)
{
int nn = dbs[bn].next_block_num;
if (nn >= 0)
{
shorten_file(nn);
}
dbs[bn].next_block_num = -1;
}
void create_fs()
{
sb.num_inodes = 10;
sb.num_blocks = 100;
sb.size_blocks = sizeof(struct disk_block);
int i;
inodes = malloc(sizeof(struct inode) * sb.num_inodes);
for (i = 0; i < sb.num_inodes; i++)
{
inodes[i].size = -1;
inodes[i].first_block = -1;
strcpy(inodes[i].name, "emptyfi");
}
dbs = malloc(sizeof(struct disk_block) * sb.num_blocks);
for (i = 0; i < sb.num_blocks; i++)
{
dbs[i].next_block_num = -1;
}
}
void mount_fs()
{
FILE *file;
file = fopen("fs_data", "r");
fread(&sb, sizeof(struct superblock), 1, file);
inodes = malloc(sizeof(struct inode) * sb.num_inodes);
dbs = malloc(sizeof(struct disk_block)* sb.num_blocks);
fread(inodes, sizeof(struct inode), sb.num_inodes, file);
fread(dbs, sizeof(struct disk_block), sb.num_blocks, file);
fclose(file);
}
void sync_fs()
{
FILE *file;
file = fopen("fs_data", "w+");
fwrite(&sb, sizeof(struct superblock), 1, file);
fwrite(inodes, sizeof(struct inode), sb.num_inodes, file);
fwrite(dbs, sizeof(struct disk_block), sb.num_blocks, file);
fclose(file);
}
int get_block_num(int file, int offset){
int togo= offset;
int bn = inodes[file].first_block;
while (togo > 0)
{
bn = dbs[bn].next_block_num;
togo--;
}
return bn;
}
void set_filesize(int filenum, int size){
int temp = size + BLOCKSIZE -1;
int num = temp / BLOCKSIZE;
int bn = inodes[filenum].first_block;
num--;
while (num > 0)
{
int next_num = dbs[bn].next_block_num;
if (next_num == -2)
{
int empty = find_empty_block();
dbs[bn].next_block_num = empty;
dbs[empty].next_block_num = -2;
}
bn = dbs[num].next_block_num;
num--;
}
shorten_file(bn);
dbs[bn].next_block_num = -2;
}
void write_byte(int filenum,int pos, char* data){
int relative_block = pos / BLOCKSIZE;
int bn = get_block_num(filenum, relative_block);
int offset = pos % BLOCKSIZE;
for (int i = 0; i < strlen(data); i++)
{
dbs[bn].data[offset +i] = data[i];
}
// dbs[bn].data[offset] = *data;
}
void print_fs()
{
printf("SuperBlock Info:\n");
printf("\tnum inodes %d\n", sb.num_inodes);
printf("\tnum blocks %d\n", sb.num_blocks);
printf("\tsize blocks %d\n", sb.size_blocks);
printf("inodes\n");
int i;
for (i = 0; i < sb.num_inodes; i++)
{
printf("\tsize: %d block: %d name: %s\n", inodes[i].size, inodes[i].first_block, inodes[i].name);
}
for (i = 0; i < sb.num_blocks; i++)
{
printf("\tblock_num: %d next block %d\n", i, dbs[i].next_block_num);
}
}