/
alloc.c
330 lines (309 loc) · 6.56 KB
/
alloc.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
#include "../param.h"
#include "../systm.h"
#include "../filsys.h"
#include "../conf.h"
#include "../buf.h"
#include "../inode.h"
#include "../user.h"
/*
* iinit is called once (from main)
* very early in initialization.
* It reads the root's super block
* and initializes the current date
* from the last modified date.
*
* panic: iinit -- cannot read the super
* block. Usually because of an IO error.
*/
iinit()
{
register *cp, *bp;
(*bdevsw[rootdev.d_major].d_open)(rootdev, 1);
bp = bread(rootdev, 1);
cp = getblk(NODEV);
if(u.u_error)
panic("iinit");
bcopy(bp->b_addr, cp->b_addr, 256);
brelse(bp);
mount[0].m_bufp = cp;
mount[0].m_dev = rootdev;
cp = cp->b_addr;
cp->s_flock = 0;
cp->s_ilock = 0;
cp->s_ronly = 0;
time[0] = cp->s_time[0];
time[1] = cp->s_time[1];
}
/* --------------------------- */
/* --------------------------- */
/*
* alloc will obtain the next available
* free disk block from the free list of
* the specified device.
* The super block has up to 100 remembered
* free blocks; the last of these is read to
* obtain 100 more . . .
*
* no space on dev x/y -- when
* the free list is exhausted.
*/
alloc(dev)
{
int bno;
register *bp, *ip, *fp;
fp = getfs(dev);
while(fp->s_flock)
sleep(&fp->s_flock, PINOD);
do {
if(fp->s_nfree <= 0)
goto nospace;
bno = fp->s_free[--fp->s_nfree];
if(bno == 0)
goto nospace;
} while (badblock(fp, bno, dev));
if(fp->s_nfree <= 0) {
fp->s_flock++;
bp = bread(dev, bno);
ip = bp->b_addr;
fp->s_nfree = *ip++;
bcopy(ip, fp->s_free, 100);
brelse(bp);
fp->s_flock = 0;
wakeup(&fp->s_flock);
}
bp = getblk(dev, bno);
clrbuf(bp);
fp->s_fmod = 1;
return(bp);
nospace:
fp->s_nfree = 0;
prdev("no space", dev);
u.u_error = ENOSPC;
return(NULL);
}
/* --------------------------- */
/* --------------------------- */
/*
* place the specified disk block
* back on the free list of the
* specified device.
*/
free(dev, bno)
{
register *fp, *bp, *ip;
fp = getfs(dev);
fp->s_fmod = 1;
while(fp->s_flock)
sleep(&fp->s_flock, PINOD);
if (badblock(fp, bno, dev))
return;
if(fp->s_nfree <= 0) {
fp->s_nfree = 1;
fp->s_free[0] = 0;
}
if(fp->s_nfree >= 100) {
fp->s_flock++;
bp = getblk(dev, bno);
ip = bp->b_addr;
*ip++ = fp->s_nfree;
bcopy(fp->s_free, ip, 100);
fp->s_nfree = 0;
bwrite(bp);
fp->s_flock = 0;
wakeup(&fp->s_flock);
}
fp->s_free[fp->s_nfree++] = bno;
fp->s_fmod = 1;
}
/* --------------------------- */
/* --------------------------- */
/*
* Check that a block number is in the
* range between the I list and the size
* of the device.
* This is used mainly to check that a
* garbage file system has not been mounted.
*
* bad block on dev x/y -- not in range
*/
badblock(afp, abn, dev)
{
register struct filsys *fp;
register char *bn;
fp = afp;
bn = abn;
if (bn < fp->s_isize+2 || bn >= fp->s_fsize) {
prdev("bad block", dev);
return(1);
}
return(0);
}
/* --------------------------- */
/* --------------------------- */
/*
* Allocate an unused I node
* on the specified device.
* Used with file creation.
* The algorithm keeps up to
* 100 spare I nodes in the
* super block. When this runs out,
* a linear search through the
* I list is instituted to pick
* up 100 more.
*/
ialloc(dev)
{
register *fp, *bp, *ip;
int i, j, k, ino;
fp = getfs(dev);
while(fp->s_ilock)
sleep(&fp->s_ilock, PINOD);
loop:
if(fp->s_ninode > 0) {
ino = fp->s_inode[--fp->s_ninode];
ip = iget(dev, ino);
if (ip==NULL)
return(NULL);
if(ip->i_mode == 0) {
for(bp = &ip->i_mode; bp < &ip->i_addr[8];)
*bp++ = 0;
fp->s_fmod = 1;
return(ip);
}
/*
* Inode was allocated after all.
* Look some more.
*/
iput(ip);
goto loop;
}
fp->s_ilock++;
ino = 0;
for(i=0; i<fp->s_isize; i++) {
bp = bread(dev, i+2);
ip = bp->b_addr;
for(j=0; j<256; j=+16) {
ino++;
if(ip[j] != 0)
continue;
for(k=0; k<NINODE; k++)
if(dev==inode[k].i_dev && ino==inode[k].i_number)
goto cont;
fp->s_inode[fp->s_ninode++] = ino;
if(fp->s_ninode >= 100)
break;
cont:;
}
brelse(bp);
if(fp->s_ninode >= 100)
break;
}
fp->s_ilock = 0;
wakeup(&fp->s_ilock);
if (fp->s_ninode > 0)
goto loop;
prdev("Out of inodes", dev);
u.u_error = ENOSPC;
return(NULL);
}
/* --------------------------- */
/* --------------------------- */
/*
* Free the specified I node
* on the specified device.
* The algorithm stores up
* to 100 I nodes in the super
* block and throws away any more.
*/
ifree(dev, ino)
{
register *fp;
fp = getfs(dev);
if(fp->s_ilock)
return;
if(fp->s_ninode >= 100)
return;
fp->s_inode[fp->s_ninode++] = ino;
fp->s_fmod = 1;
}
/* --------------------------- */
/* --------------------------- */
/*
* getfs maps a device number into
* a pointer to the incore super
* block.
* The algorithm is a linear
* search through the mount table.
* A consistency check of the
* in core free-block and i-node
* counts.
*
* bad count on dev x/y -- the count
* check failed. At this point, all
* the counts are zeroed which will
* almost certainly lead to "no space"
* diagnostic
* panic: no fs -- the device is not mounted.
* this "cannot happen"
*/
getfs(dev)
{
register struct mount *p;
register char *n1, *n2;
for(p = &mount[0]; p < &mount[NMOUNT]; p++)
if(p->m_bufp != NULL && p->m_dev == dev) {
p = p->m_bufp->b_addr;
n1 = p->s_nfree;
n2 = p->s_ninode;
if(n1 > 100 || n2 > 100) {
prdev("bad count", dev);
p->s_nfree = 0;
p->s_ninode = 0;
}
return(p);
}
panic("no fs");
}
/* --------------------------- */
/* --------------------------- */
/*
* update is the internal name of
* 'sync'. It goes through the disk
* queues to initiate sandbagged IO;
* goes through the I nodes to write
* modified nodes; and it goes through
* the mount table to initiate modified
* super blocks.
*/
update()
{
register struct inode *ip;
register struct mount *mp;
register *bp;
if(updlock)
return;
updlock++;
for(mp = &mount[0]; mp < &mount[NMOUNT]; mp++)
if(mp->m_bufp != NULL) {
ip = mp->m_bufp->b_addr;
if(ip->s_fmod==0 || ip->s_ilock!=0 ||
ip->s_flock!=0 || ip->s_ronly!=0)
continue;
bp = getblk(mp->m_dev, 1);
ip->s_fmod = 0;
ip->s_time[0] = time[0];
ip->s_time[1] = time[1];
bcopy(ip, bp->b_addr, 256);
bwrite(bp);
}
for(ip = &inode[0]; ip < &inode[NINODE]; ip++)
if((ip->i_flag&ILOCK) == 0) {
ip->i_flag =| ILOCK;
iupdat(ip, time);
prele(ip);
}
updlock = 0;
bflush(NODEV);
}
/* --------------------------- */
/* --------------------------- */