forked from wesedens/fs-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
229 lines (172 loc) · 5.51 KB
/
util.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
/*
This file contains some utility routines that may be useful if you
need to get temporary blocks of storage in your file system.
It also contains the myfs_die() routine which is a sort of general
purpose panic type function that will halt the file system.
THIS CODE COPYRIGHT DOMINIC GIAMPAOLO. NO WARRANTY IS EXPRESSED
OR IMPLIED. YOU MAY USE THIS CODE AND FREELY DISTRIBUTE IT FOR
NON-COMMERCIAL USE AS LONG AS THIS NOTICE REMAINS ATTACHED.
FOR COMMERCIAL USE, CONTACT DOMINIC GIAMPAOLO (dbg@be.com).
Dominic Giampaolo
dbg@be.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include "myfs.h"
int
init_tmp_blocks(myfs_info *myfs)
{
int i;
tmp_blocks *tmp;
myfs->tmp_blocks_sem = create_sem(1, "tmp_blocks_sem");
if (myfs->tmp_blocks_sem < 0)
return ENOMEM;
myfs->tmp_blocks = (tmp_blocks *)malloc(sizeof(tmp_blocks));
if (myfs->tmp_blocks == NULL)
goto err1;
tmp = myfs->tmp_blocks;
tmp->next = NULL;
tmp->data = (char *)malloc(NUM_TMP_BLOCKS * myfs->dsb.block_size);
if (tmp->data == NULL)
goto err2;
for(i=0; i < NUM_TMP_BLOCKS; i++)
tmp->block_ptrs[i] = tmp->data + (i * myfs->dsb.block_size);
return 0;
err2:
free(myfs->tmp_blocks);
myfs->tmp_blocks = NULL;
err1:
delete_sem(myfs->tmp_blocks_sem);
myfs->tmp_blocks_sem = -1;
return ENOMEM;
}
void
shutdown_tmp_blocks(myfs_info *myfs)
{
int i;
tmp_blocks *tmp, *next;
for(tmp=myfs->tmp_blocks; tmp; tmp=next) {
for(i=0; i < NUM_TMP_BLOCKS; i++)
if (tmp->block_ptrs[i] == NULL)
printf("unfree'd tmp block @ 0x%lx index %d\n",
(ulong)(tmp->data + i * myfs->dsb.block_size), i);
free(tmp->data);
next = tmp->next;
free(tmp);
}
if (myfs->tmp_blocks_sem > 0)
delete_sem(myfs->tmp_blocks_sem);
myfs->tmp_blocks_sem = -1;
}
char *
get_tmp_blocks(myfs_info *myfs, int nblocks)
{
int i, j, start;
char *blocks;
tmp_blocks *tmp;
if (nblocks > NUM_TMP_BLOCKS) {
printf("*** error: requested %d tmp blocks but can only have %d\n",
nblocks, NUM_TMP_BLOCKS);
return NULL;
}
acquire_sem(myfs->tmp_blocks_sem);
for(tmp=myfs->tmp_blocks; tmp; tmp=tmp->next) {
blocks = NULL;
start = -999999;
for(i=0; i < NUM_TMP_BLOCKS; i++) {
if (blocks == NULL && tmp->block_ptrs[i]) {
start = i;
blocks = tmp->block_ptrs[i];
}
if (blocks && tmp->block_ptrs[i] != NULL &&
((i + 1) - start) == nblocks) {
for(j=start; j <= i; j++) {
if (tmp->block_ptrs[j] == NULL) {
myfs_die("you fucking whore of the revelation\n");
}
tmp->block_ptrs[j] = NULL;
}
release_sem(myfs->tmp_blocks_sem);
return blocks;
} else if (tmp->block_ptrs[i] == NULL) {
/* reset our pointers and keep on truckin' */
blocks = NULL;
start = -9999999;
}
}
}
/*
if we get here then we couldn't find any free space and we have
to go allocate some with malloc.
*/
tmp = (tmp_blocks *)malloc(sizeof(tmp_blocks));
if (tmp == NULL)
return NULL;
tmp->data = (void *)malloc(NUM_TMP_BLOCKS * myfs->dsb.block_size);
if (tmp->data == NULL) {
free(tmp);
return NULL;
}
for(i=0; i < nblocks; i++)
tmp->block_ptrs[i] = NULL;
for(; i < NUM_TMP_BLOCKS; i++)
tmp->block_ptrs[i] = tmp->data + (i * myfs->dsb.block_size);
tmp->next = myfs->tmp_blocks;
myfs->tmp_blocks = tmp;
release_sem(myfs->tmp_blocks_sem);
return tmp->data;
}
void
free_tmp_blocks(myfs_info *myfs, char *blocks, int nblocks)
{
int i, j;
char *end;
tmp_blocks *tmp;
if (blocks == NULL)
myfs_die("free_tmp_blocks called w/null block ptr (%d)\n", nblocks);
if (nblocks > NUM_TMP_BLOCKS)
myfs_die("*** free_tmp_blocks: nblocks == %d but should be < %d\n",
nblocks, NUM_TMP_BLOCKS);
acquire_sem(myfs->tmp_blocks_sem);
for(tmp=myfs->tmp_blocks; tmp; tmp=tmp->next) {
end = tmp->data + NUM_TMP_BLOCKS * myfs->dsb.block_size;
if (blocks >= tmp->data && blocks < end) { /* we have a match! */
i = (blocks - tmp->data) / myfs->dsb.block_size;
for(j=0; j < nblocks; j++, blocks+=myfs->dsb.block_size) {
if (tmp->block_ptrs[i + j] != NULL) {
myfs_die("free_tmp_blocks ptr 0x%x already free?!?\n",
tmp->block_ptrs[i + j]);
break;
}
tmp->block_ptrs[i + j] = blocks;
}
break;
}
}
if (tmp == NULL)
myfs_die("free_tmp_blocks: failed to free %d blocks @ 0x%x\n",
nblocks, blocks);
release_sem(myfs->tmp_blocks_sem);
}
int
myfs_die(const char *fmt, ...)
{
va_list ap;
static char buff[512];
printf("\n");
va_start(ap, fmt);
vsprintf(buff, fmt, ap);
va_end(ap);
printf("%s\n", buff);
printf("spinning forever.\n");
while(1)
;
return 0;
}