-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsInit.c
More file actions
271 lines (216 loc) · 5.64 KB
/
Copy pathfsInit.c
File metadata and controls
271 lines (216 loc) · 5.64 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
/**************************************************************
* Class:: CSC-415-01 Summer 2025
* Name:: Anmol Tadikonda, Yuhang Wei, Bryan Escobar, Edgar Jaquez
* Student IDs:: 922643511, 923749629, 922876796, 923919292
* GitHub-Name:: csc415-filesystem-JustinWei110
* Group-Name:: Operating System 2
* Project:: Basic File System
*
* File:: fsInit.c
*
* Description:: The implementation for the main driver for file system assignment.
*
*
*
**************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include "fsLow.h"
#include "fsInit.h"
#include "mfs.h"
#include <math.h>
static uint32_t mapStart = 0;
static uint32_t mapLength = 0;
static uint32_t tolBlocks = 0;
static uint32_t blockSize = 0;
int deallocate_blocks(uint32_t blockStart, uint32_t size) {
uint8_t* bitmap = NULL;
uint32_t numBlocks = ceil(size / blockSize);
bitmap = malloc(mapLength * blockSize);
if (!bitmap)
{
perror("Failed to load bitmap");
return 1;
}
LBAread(bitmap, mapLength, mapStart);
for (uint32_t i = blockStart; i < blockStart + numBlocks; i++) {
if (i > tolBlocks) {
printf("Tried to deallocate out of disk");
return 1;
}
int byte = i / 8;
int bit = i % 8;
bitmap[byte] &= ~(1 << bit);
}
LBAwrite(bitmap, mapLength, mapStart);
free(bitmap);
return 0;
}
uint32_t allocate_blocks(uint32_t numBlocks) {
uint8_t* bitmap = NULL;
//loading bitmap
bitmap = malloc(mapLength * blockSize);
if (!bitmap)
{
perror("Failed to load bitmap");
return 1;
}
LBAread(bitmap, mapLength, mapStart);
// free blocks
uint32_t startBlock = 0;
uint32_t Freeblocks = 0;
//find free blocks
for (uint32_t i = 0; i < tolBlocks; i++) {
int byte = i / 8;
int bit = i % 8;
//checking for free blocks
if (!(bitmap[byte] & (1 << bit))) {
Freeblocks++;
if (Freeblocks == numBlocks) {
startBlock = i - numBlocks + 1;
break;
}
}
else {
Freeblocks = 0;
}
}
if (Freeblocks < numBlocks) {
return -1;
}
//used blocks
for (uint32_t i = startBlock; i < startBlock + numBlocks; i++) {
int bytes = i / 8;
int bits = i % 8;
bitmap[bytes] |= (1 << bits);
}
//updates bitmap
LBAwrite(bitmap, mapLength, mapStart);
free(bitmap);
return startBlock;
}
int initFreeSpace(uint64_t numberOfBlocks) {
int bitMapNumBlocks = ceil(((float)numberOfBlocks / 8.0f) / (float)blockSize);
u_int8_t* spaceBitMap = calloc(1, blockSize * bitMapNumBlocks);
if (!spaceBitMap) {
perror("Can't allocate bitmap");
return 1;
}
for (int i = 0; i < 6; i++) {
int index = i / 8;
int offset = i % 8;
spaceBitMap[index] |= (1 << offset);
}
uint64_t write = LBAwrite(spaceBitMap, bitMapNumBlocks, 1);
if (write != bitMapNumBlocks) {
perror("Can't write bit map to disk");
free(spaceBitMap);
return 1;
}
free(spaceBitMap);
return bitMapNumBlocks;
}
int initFileSystem(uint64_t numberOfBlocks, uint64_t givenBlockSize)
{
blockSize = givenBlockSize;
printf("Initializing File System with %ld blocks with a block size of %ld\n", numberOfBlocks, givenBlockSize);
struct VCBtype* VCB = NULL;
VCB = malloc(givenBlockSize);
if (!VCB)
{
perror("Can't allocate VCB");
return 1;
}
// Check if first block actually contains a valid VCB
LBAread(VCB, 1, 0);
if (VCB->signature != PART_SIGNATURE && VCB->signature != PART_SIGNATURE2) {
// If it doesn't then initialize it
VCB->numberOfBlocks = numberOfBlocks;
VCB->blockSize = givenBlockSize;
VCB->signature = PART_SIGNATURE;
// Initialize free space
int bitMapNumBlocks = initFreeSpace(numberOfBlocks);
mapStart = 1;
mapLength = bitMapNumBlocks;
tolBlocks = numberOfBlocks;
VCB->mapStart = 1;
VCB->mapLength = bitMapNumBlocks;
// Initialize root directory
// *TO DO*
uint32_t entries = 50;
uint32_t size = 60;
uint32_t bytes = entries * size;
uint32_t blocksNeed = ceil((double)bytes / (double)blockSize);
// memory for directory entries
void* rootEntries = malloc(blocksNeed * blockSize);
if (!rootEntries)
{
perror("Failed to allocate entries");
free(VCB);
return 1;
}
//Initialize to 0
memset(rootEntries, 0, blocksNeed * blockSize);
mapStart = VCB->mapStart;
mapLength = VCB->mapLength;
tolBlocks = VCB->numberOfBlocks;
blockSize = VCB->blockSize;
//get blocks from free space
uint32_t StartBlock = allocate_blocks(blocksNeed);
if (StartBlock == (uint32_t)-1) {
perror("Failed to allocate blocks");
free(rootEntries);
free(VCB);
return 1;
}
// set "." beginning of rootEntries
struct DirectoryEntry* entry = (struct DirectoryEntry*)rootEntries;
strncpy(entry[0].name, ".", 32);
entry[0].fileSize = (entries - 2) * size;
entry[0].inWhichBlock = StartBlock;
entry[0].createdTime = time(NULL);
entry[0].isDirectory = 1;
//setting entry ".."
strncpy(entry[1].name, "..", 32);
entry[1].fileSize = entry[0].fileSize;
entry[1].inWhichBlock = StartBlock;
entry[1].createdTime = entry[0].createdTime;
entry[1].isDirectory = 1;
//root directory to disk
if (LBAwrite(rootEntries, blocksNeed, StartBlock) != blocksNeed)
{
perror("Failed to write root directory");
free(rootEntries);
free(VCB);
return 1;
}
//VCB with root directory Location
VCB->rootDir = StartBlock;
u_int64_t write = LBAwrite(VCB, 1, 0);
if (write != 1)
{
perror("Can't write VCB to disk");
free(rootEntries);
free(VCB);
return 1;
}
free(rootEntries);
}
else {
mapStart = VCB->mapStart;
mapLength = VCB->mapLength;
tolBlocks = VCB->numberOfBlocks;
blockSize = VCB->blockSize;
}
free(VCB);
return 0;
}
void exitFileSystem()
{
printf("System exiting\n");
}