-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsFreeSpace.c
More file actions
201 lines (167 loc) · 5.27 KB
/
fsFreeSpace.c
File metadata and controls
201 lines (167 loc) · 5.27 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
/**************************************************************
* Class:: CSC-415-02 Summer 2025
* Name:: Phillip Davis, Igor Tello Jared Aung, Preet Vithani
* Student IDs:: 923980431, 923043807, 922772159, 923575806
* GitHub-Name:: R3plug
* Group-Name:: Team Kentucky Kernels
* Project:: Basic File System
*
* File:: fsFreeSpace.c
*
* Description:: Free Space manager
*
**************************************************************/
#include "fsFreeSpace.h"
#include "fsLow.h"
#include <stdio.h>
#include <stdlib.h>
//#include "fsPath.h"
#include "fsInit.h"
VCB *vcb = NULL;
int loadVCB()
{
vcb = malloc(sizeof(VCB));
if (!vcb)
return -1;
if (LBAread(vcb, 1, 0) != 1)
{
free(vcb);
vcb = NULL;
return -1;
}
return 0;
}
Extent *allocateFreeBlocks(uint32_t minExtentLength, uint32_t *extentsAllocated)
{
// Load extent table from disk
ExtentTable *table = (ExtentTable *)malloc(EXTENT_TABLE_BLOCKS * BLOCK_SIZE);
if (!table)
{
perror("Failed to allocate memory for extent table");
return NULL;
}
if (LBAread(table, EXTENT_TABLE_BLOCKS, 1) != EXTENT_TABLE_BLOCKS)
{
perror("Failed to read extent table from disk");
free(table);
return NULL;
}
// lets now allocate spcae to store the extends we will be returning
Extent *allocatedExtents = calloc(EXTENT_TABLE_BLOCKS, sizeof(Extent));
if (!allocatedExtents)
{
perror("Failed to allocate memory for result extents");
return NULL;
}
uint32_t resultIndex = 0;
for (int i = 0; i < table->extentCount; i++)
{
Extent *extent = &table->extents[i];
// we need to look for free extends that meet the size
if (extent->used == 0 && extent->count >= minExtentLength)
{
/*
// we add to the result array
allocatedExtents[resultIndex++] = (Extent){
.block = extent->block,
.count = extent->count,
.used = 1};
// lets remove the extent from the table
for (int j = i; j < table->extentCount - 1; j++)
{
table->extents[j] = table->extents[j + 1];
}
table->extentCount--;
i--; // lets check the current index after shifting
*/
// we add to the result array
allocatedExtents[resultIndex++] = (Extent){
.block = extent->block,
.count = minExtentLength,
.used = 1};
// lets remove the extent from the table
// set the startof the extent to the end of allcoated section
table->extents[i].block += minExtentLength;
break;
}
}
if (resultIndex == 0)
{
// if no extends is allocated
// lets free the extend and return NULL
*extentsAllocated = 0;
free(allocatedExtents);
free(table);
return NULL;
}
LBAwrite(table, EXTENT_TABLE_BLOCKS, 1);
// printf("Allocated Extents %d\n", allocatedExtents->count);
// lets return the allocated extents and count
*extentsAllocated = resultIndex;
free(table);
return allocatedExtents;
}
int releaseBlocks(uint32_t startBlock, uint32_t blockCount)
{
static ExtentTable *freeTable = NULL;
if (!freeTable)
{
freeTable = malloc(sizeof(ExtentTable));
if (LBAread(freeTable, EXTENT_TABLE_BLOCKS, vcb->extentTableStart) != EXTENT_TABLE_BLOCKS)
{
free(freeTable);
return -1;
}
}
for (int i = 0; i < EXTENT_TABLE_BLOCKS; i++)
{
if (freeTable->extents[i].block == startBlock &&
freeTable->extents[i].count == blockCount)
{
freeTable->extents[i].used = 0;
break;
}
}
if (LBAwrite(freeTable, EXTENT_TABLE_BLOCKS, vcb->extentTableStart) != EXTENT_TABLE_BLOCKS)
{
perror("LBAwrite freeTable failed");
return -1;
}
return 0;
}
int initFreeSpace(uint64_t numberOfBlocks, uint64_t blockSize)
{
ExtentTable *extentTable = (ExtentTable *)calloc(1, EXTENT_TABLE_BLOCKS * blockSize);
if (extentTable == NULL)
{
perror("Failed to allocate memory for extent table");
return -1;
}
int index = 0;
// Block 0 for VCB
extentTable->extents[index++] = (Extent){.block = 0, .count = 1, .used = 1};
// Extent table blocks
extentTable->extents[index++] = (Extent){.block = 1, .count = EXTENT_TABLE_BLOCKS, .used = 1};
// Root directory blocks
extentTable->extents[index++] = (Extent){
.block = 1 + EXTENT_TABLE_BLOCKS,
.count = ROOT_DIRECTORY_BLOCKS,
.used = 1};
uint32_t freeSpaceBlocks = 1 + EXTENT_TABLE_BLOCKS + ROOT_DIRECTORY_BLOCKS;
uint32_t remaining = numberOfBlocks - freeSpaceBlocks;
// Rest of blocks are free space
extentTable->extents[index++] = (Extent){.block = freeSpaceBlocks, .count = remaining, .used = 0};
extentTable->extentCount = index;
LBAwrite(extentTable, EXTENT_TABLE_BLOCKS, 1);
free(extentTable);
return 0;
}
VCB *getVCB(void) {
VCB *vcb = malloc(sizeof(VCB));
if (!vcb) return NULL;
if ( LBAread(vcb, 1, 0) != 1 ) {
free(vcb);
return NULL;
}
return vcb;
}