Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
0880b18e | 2 | * Copyright (c) 1982, 1986 Regents of the University of California. |
da7c5cc6 KM |
3 | * All rights reserved. The Berkeley software License Agreement |
4 | * specifies the terms and conditions for redistribution. | |
5 | * | |
0880b18e | 6 | * @(#)ffs_alloc.c 7.1 (Berkeley) %G% |
da7c5cc6 | 7 | */ |
e3fe2d69 | 8 | |
94368568 JB |
9 | #include "param.h" |
10 | #include "systm.h" | |
11 | #include "mount.h" | |
12 | #include "fs.h" | |
94368568 JB |
13 | #include "buf.h" |
14 | #include "inode.h" | |
15 | #include "dir.h" | |
16 | #include "user.h" | |
17 | #include "quota.h" | |
18 | #include "kernel.h" | |
7580177b | 19 | #include "syslog.h" |
aec7dd3b | 20 | #include "cmap.h" |
e3fe2d69 | 21 | |
daaf7bee | 22 | extern u_long hashalloc(); |
4f083fd7 SL |
23 | extern ino_t ialloccg(); |
24 | extern daddr_t alloccg(); | |
743f1ef7 KM |
25 | extern daddr_t alloccgblk(); |
26 | extern daddr_t fragextend(); | |
27 | extern daddr_t blkpref(); | |
28 | extern daddr_t mapsearch(); | |
1d7a08c5 | 29 | extern int inside[], around[]; |
b6407c9d | 30 | extern unsigned char *fragtbl[]; |
e3fe2d69 | 31 | |
502770a3 KM |
32 | /* |
33 | * Allocate a block in the file system. | |
34 | * | |
35 | * The size of the requested block is given, which must be some | |
36 | * multiple of fs_fsize and <= fs_bsize. | |
37 | * A preference may be optionally specified. If a preference is given | |
38 | * the following hierarchy is used to allocate a block: | |
39 | * 1) allocate the requested block. | |
40 | * 2) allocate a rotationally optimal block in the same cylinder. | |
41 | * 3) allocate a block in the same cylinder group. | |
42 | * 4) quadradically rehash into other cylinder groups, until an | |
43 | * available block is located. | |
44 | * If no block preference is given the following heirarchy is used | |
45 | * to allocate a block: | |
46 | * 1) allocate a block in the cylinder group that contains the | |
47 | * inode for the file. | |
48 | * 2) quadradically rehash into other cylinder groups, until an | |
49 | * available block is located. | |
50 | */ | |
e3fe2d69 | 51 | struct buf * |
f7287e4b | 52 | alloc(ip, bpref, size) |
f3c028b7 | 53 | register struct inode *ip; |
e3fe2d69 KM |
54 | daddr_t bpref; |
55 | int size; | |
56 | { | |
57 | daddr_t bno; | |
58 | register struct fs *fs; | |
f3c028b7 | 59 | register struct buf *bp; |
e3fe2d69 KM |
60 | int cg; |
61 | ||
f7287e4b | 62 | fs = ip->i_fs; |
ffd90e52 KM |
63 | if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { |
64 | printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", | |
65 | ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); | |
b6407c9d | 66 | panic("alloc: bad size"); |
ffd90e52 | 67 | } |
b6407c9d | 68 | if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0) |
0947395d | 69 | goto nospace; |
240a4664 | 70 | if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) |
e3fe2d69 | 71 | goto nospace; |
b4567e9c | 72 | #ifdef QUOTA |
2523b389 SL |
73 | u.u_error = chkdq(ip, (long)btodb(size), 0); |
74 | if (u.u_error) | |
75 | return (NULL); | |
ca90a6bf | 76 | #endif |
260e5e3c KM |
77 | if (bpref >= fs->fs_size) |
78 | bpref = 0; | |
e3fe2d69 | 79 | if (bpref == 0) |
6994bf5d | 80 | cg = itog(fs, ip->i_number); |
e3fe2d69 | 81 | else |
6994bf5d | 82 | cg = dtog(fs, bpref); |
4f083fd7 SL |
83 | bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size, |
84 | (u_long (*)())alloccg); | |
6459ebe0 | 85 | if (bno <= 0) |
e3fe2d69 | 86 | goto nospace; |
2523b389 SL |
87 | ip->i_blocks += btodb(size); |
88 | ip->i_flag |= IUPD|ICHG; | |
f7287e4b | 89 | bp = getblk(ip->i_dev, fsbtodb(fs, bno), size); |
e3fe2d69 KM |
90 | clrbuf(bp); |
91 | return (bp); | |
92 | nospace: | |
93 | fserr(fs, "file system full"); | |
94 | uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); | |
95 | u.u_error = ENOSPC; | |
96 | return (NULL); | |
97 | } | |
98 | ||
502770a3 KM |
99 | /* |
100 | * Reallocate a fragment to a bigger size | |
101 | * | |
102 | * The number and size of the old block is given, and a preference | |
103 | * and new size is also specified. The allocator attempts to extend | |
104 | * the original block. Failing that, the regular block allocator is | |
105 | * invoked to get an appropriate block. | |
106 | */ | |
07670f7d | 107 | struct buf * |
f7287e4b KM |
108 | realloccg(ip, bprev, bpref, osize, nsize) |
109 | register struct inode *ip; | |
743f1ef7 | 110 | daddr_t bprev, bpref; |
07670f7d KM |
111 | int osize, nsize; |
112 | { | |
07670f7d | 113 | register struct fs *fs; |
f3c028b7 | 114 | register struct buf *bp, *obp; |
f7fa0c54 | 115 | int cg, request; |
5d934daa KM |
116 | daddr_t bno, bn; |
117 | int i, count, s; | |
118 | extern struct cmap *mfind(); | |
07670f7d | 119 | |
f7287e4b | 120 | fs = ip->i_fs; |
d995d89d | 121 | if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 || |
ffd90e52 KM |
122 | (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) { |
123 | printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n", | |
124 | ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt); | |
b6407c9d | 125 | panic("realloccg: bad size"); |
ffd90e52 | 126 | } |
240a4664 | 127 | if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0) |
0947395d | 128 | goto nospace; |
ffd90e52 KM |
129 | if (bprev == 0) { |
130 | printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n", | |
131 | ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt); | |
502770a3 | 132 | panic("realloccg: bad bprev"); |
ffd90e52 | 133 | } |
b4567e9c | 134 | #ifdef QUOTA |
2523b389 SL |
135 | u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0); |
136 | if (u.u_error) | |
137 | return (NULL); | |
ca90a6bf | 138 | #endif |
ae851115 | 139 | cg = dtog(fs, bprev); |
f7287e4b | 140 | bno = fragextend(ip, cg, (long)bprev, osize, nsize); |
f3c028b7 | 141 | if (bno != 0) { |
9d6d37ce BJ |
142 | do { |
143 | bp = bread(ip->i_dev, fsbtodb(fs, bno), osize); | |
144 | if (bp->b_flags & B_ERROR) { | |
145 | brelse(bp); | |
146 | return (NULL); | |
147 | } | |
148 | } while (brealloc(bp, nsize) == 0); | |
149 | bp->b_flags |= B_DONE; | |
4f083fd7 | 150 | bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); |
2523b389 SL |
151 | ip->i_blocks += btodb(nsize - osize); |
152 | ip->i_flag |= IUPD|ICHG; | |
f3c028b7 KM |
153 | return (bp); |
154 | } | |
260e5e3c KM |
155 | if (bpref >= fs->fs_size) |
156 | bpref = 0; | |
aec7dd3b | 157 | switch ((int)fs->fs_optim) { |
f8484b5f KM |
158 | case FS_OPTSPACE: |
159 | /* | |
160 | * Allocate an exact sized fragment. Although this makes | |
161 | * best use of space, we will waste time relocating it if | |
162 | * the file continues to grow. If the fragmentation is | |
163 | * less than half of the minimum free reserve, we choose | |
164 | * to begin optimizing for time. | |
165 | */ | |
f7fa0c54 | 166 | request = nsize; |
f8484b5f KM |
167 | if (fs->fs_minfree < 5 || |
168 | fs->fs_cstotal.cs_nffree > | |
169 | fs->fs_dsize * fs->fs_minfree / (2 * 100)) | |
170 | break; | |
171 | log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n", | |
172 | fs->fs_fsmnt); | |
173 | fs->fs_optim = FS_OPTTIME; | |
174 | break; | |
175 | case FS_OPTTIME: | |
176 | /* | |
177 | * At this point we have discovered a file that is trying | |
178 | * to grow a small fragment to a larger fragment. To save | |
179 | * time, we allocate a full sized block, then free the | |
180 | * unused portion. If the file continues to grow, the | |
181 | * `fragextend' call above will be able to grow it in place | |
182 | * without further copying. If aberrant programs cause | |
183 | * disk fragmentation to grow within 2% of the free reserve, | |
184 | * we choose to begin optimizing for space. | |
185 | */ | |
f7fa0c54 | 186 | request = fs->fs_bsize; |
f8484b5f KM |
187 | if (fs->fs_cstotal.cs_nffree < |
188 | fs->fs_dsize * (fs->fs_minfree - 2) / 100) | |
189 | break; | |
190 | log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n", | |
191 | fs->fs_fsmnt); | |
192 | fs->fs_optim = FS_OPTSPACE; | |
193 | break; | |
194 | default: | |
195 | printf("dev = 0x%x, optim = %d, fs = %s\n", | |
196 | ip->i_dev, fs->fs_optim, fs->fs_fsmnt); | |
197 | panic("realloccg: bad optim"); | |
198 | /* NOTREACHED */ | |
199 | } | |
f7fa0c54 | 200 | bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request, |
4f083fd7 | 201 | (u_long (*)())alloccg); |
6459ebe0 | 202 | if (bno > 0) { |
f7287e4b | 203 | obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize); |
d995d89d KM |
204 | if (obp->b_flags & B_ERROR) { |
205 | brelse(obp); | |
ae851115 | 206 | return (NULL); |
d995d89d | 207 | } |
5d934daa KM |
208 | bn = fsbtodb(fs, bno); |
209 | bp = getblk(ip->i_dev, bn, nsize); | |
260ede8e | 210 | bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize); |
5d934daa KM |
211 | count = howmany(osize, DEV_BSIZE); |
212 | s = splimp(); | |
213 | for (i = 0; i < count; i += CLBYTES / DEV_BSIZE) | |
214 | if (mfind(ip->i_dev, bn + i)) | |
215 | munhash(ip->i_dev, bn + i); | |
216 | splx(s); | |
260ede8e KM |
217 | bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize); |
218 | if (obp->b_flags & B_DELWRI) { | |
219 | obp->b_flags &= ~B_DELWRI; | |
220 | u.u_ru.ru_oublock--; /* delete charge */ | |
221 | } | |
f3c028b7 | 222 | brelse(obp); |
4f083fd7 | 223 | free(ip, bprev, (off_t)osize); |
f7fa0c54 | 224 | if (nsize < request) |
e8727f9b | 225 | free(ip, bno + numfrags(fs, nsize), |
f7fa0c54 | 226 | (off_t)(request - nsize)); |
2523b389 SL |
227 | ip->i_blocks += btodb(nsize - osize); |
228 | ip->i_flag |= IUPD|ICHG; | |
ae851115 | 229 | return (bp); |
f3c028b7 | 230 | } |
0947395d | 231 | nospace: |
f3c028b7 KM |
232 | /* |
233 | * no space available | |
234 | */ | |
07670f7d KM |
235 | fserr(fs, "file system full"); |
236 | uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt); | |
237 | u.u_error = ENOSPC; | |
238 | return (NULL); | |
239 | } | |
240 | ||
502770a3 KM |
241 | /* |
242 | * Allocate an inode in the file system. | |
243 | * | |
244 | * A preference may be optionally specified. If a preference is given | |
245 | * the following hierarchy is used to allocate an inode: | |
246 | * 1) allocate the requested inode. | |
247 | * 2) allocate an inode in the same cylinder group. | |
248 | * 3) quadradically rehash into other cylinder groups, until an | |
249 | * available inode is located. | |
250 | * If no inode preference is given the following heirarchy is used | |
251 | * to allocate an inode: | |
252 | * 1) allocate an inode in cylinder group 0. | |
253 | * 2) quadradically rehash into other cylinder groups, until an | |
254 | * available inode is located. | |
255 | */ | |
e3fe2d69 | 256 | struct inode * |
f7287e4b KM |
257 | ialloc(pip, ipref, mode) |
258 | register struct inode *pip; | |
e3fe2d69 KM |
259 | ino_t ipref; |
260 | int mode; | |
261 | { | |
daaf7bee | 262 | ino_t ino; |
e3fe2d69 KM |
263 | register struct fs *fs; |
264 | register struct inode *ip; | |
265 | int cg; | |
266 | ||
f7287e4b | 267 | fs = pip->i_fs; |
0947395d | 268 | if (fs->fs_cstotal.cs_nifree == 0) |
e3fe2d69 | 269 | goto noinodes; |
b4567e9c | 270 | #ifdef QUOTA |
2523b389 SL |
271 | u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0); |
272 | if (u.u_error) | |
273 | return (NULL); | |
ca90a6bf | 274 | #endif |
260e5e3c KM |
275 | if (ipref >= fs->fs_ncg * fs->fs_ipg) |
276 | ipref = 0; | |
6994bf5d | 277 | cg = itog(fs, ipref); |
f7287e4b | 278 | ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg); |
e3fe2d69 KM |
279 | if (ino == 0) |
280 | goto noinodes; | |
f7287e4b | 281 | ip = iget(pip->i_dev, pip->i_fs, ino); |
e3fe2d69 | 282 | if (ip == NULL) { |
bcd8515e | 283 | ifree(pip, ino, 0); |
e3fe2d69 KM |
284 | return (NULL); |
285 | } | |
ffd90e52 KM |
286 | if (ip->i_mode) { |
287 | printf("mode = 0%o, inum = %d, fs = %s\n", | |
288 | ip->i_mode, ip->i_number, fs->fs_fsmnt); | |
e3fe2d69 | 289 | panic("ialloc: dup alloc"); |
ffd90e52 | 290 | } |
2523b389 SL |
291 | if (ip->i_blocks) { /* XXX */ |
292 | printf("free inode %s/%d had %d blocks\n", | |
293 | fs->fs_fsmnt, ino, ip->i_blocks); | |
294 | ip->i_blocks = 0; | |
295 | } | |
e3fe2d69 KM |
296 | return (ip); |
297 | noinodes: | |
298 | fserr(fs, "out of inodes"); | |
ae851115 | 299 | uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt); |
e3fe2d69 KM |
300 | u.u_error = ENOSPC; |
301 | return (NULL); | |
302 | } | |
303 | ||
743f1ef7 | 304 | /* |
502770a3 KM |
305 | * Find a cylinder to place a directory. |
306 | * | |
307 | * The policy implemented by this algorithm is to select from | |
308 | * among those cylinder groups with above the average number of | |
309 | * free inodes, the one with the smallest number of directories. | |
743f1ef7 | 310 | */ |
4f083fd7 | 311 | ino_t |
f7287e4b | 312 | dirpref(fs) |
e3fe2d69 | 313 | register struct fs *fs; |
f7287e4b | 314 | { |
743f1ef7 | 315 | int cg, minndir, mincg, avgifree; |
e3fe2d69 | 316 | |
0947395d | 317 | avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg; |
743f1ef7 | 318 | minndir = fs->fs_ipg; |
e3fe2d69 | 319 | mincg = 0; |
743f1ef7 | 320 | for (cg = 0; cg < fs->fs_ncg; cg++) |
b6407c9d KM |
321 | if (fs->fs_cs(fs, cg).cs_ndir < minndir && |
322 | fs->fs_cs(fs, cg).cs_nifree >= avgifree) { | |
e3fe2d69 | 323 | mincg = cg; |
b6407c9d | 324 | minndir = fs->fs_cs(fs, cg).cs_ndir; |
e3fe2d69 | 325 | } |
4f083fd7 | 326 | return ((ino_t)(fs->fs_ipg * mincg)); |
e3fe2d69 KM |
327 | } |
328 | ||
743f1ef7 | 329 | /* |
4f083fd7 SL |
330 | * Select the desired position for the next block in a file. The file is |
331 | * logically divided into sections. The first section is composed of the | |
332 | * direct blocks. Each additional section contains fs_maxbpg blocks. | |
333 | * | |
334 | * If no blocks have been allocated in the first section, the policy is to | |
335 | * request a block in the same cylinder group as the inode that describes | |
336 | * the file. If no blocks have been allocated in any other section, the | |
337 | * policy is to place the section in a cylinder group with a greater than | |
338 | * average number of free blocks. An appropriate cylinder group is found | |
16e7863f KM |
339 | * by using a rotor that sweeps the cylinder groups. When a new group of |
340 | * blocks is needed, the sweep begins in the cylinder group following the | |
341 | * cylinder group from which the previous allocation was made. The sweep | |
342 | * continues until a cylinder group with greater than the average number | |
343 | * of free blocks is found. If the allocation is for the first block in an | |
344 | * indirect block, the information on the previous allocation is unavailable; | |
345 | * here a best guess is made based upon the logical block number being | |
346 | * allocated. | |
4f083fd7 SL |
347 | * |
348 | * If a section is already partially allocated, the policy is to | |
349 | * contiguously allocate fs_maxcontig blocks. The end of one of these | |
350 | * contiguous blocks and the beginning of the next is physically separated | |
351 | * so that the disk head will be in transit between them for at least | |
352 | * fs_rotdelay milliseconds. This is to allow time for the processor to | |
353 | * schedule another I/O transfer. | |
743f1ef7 | 354 | */ |
daaf7bee | 355 | daddr_t |
4f083fd7 SL |
356 | blkpref(ip, lbn, indx, bap) |
357 | struct inode *ip; | |
358 | daddr_t lbn; | |
359 | int indx; | |
360 | daddr_t *bap; | |
f7287e4b | 361 | { |
4f083fd7 | 362 | register struct fs *fs; |
16e7863f KM |
363 | register int cg; |
364 | int avgbfree, startcg; | |
4f083fd7 | 365 | daddr_t nextblk; |
743f1ef7 | 366 | |
4f083fd7 SL |
367 | fs = ip->i_fs; |
368 | if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) { | |
369 | if (lbn < NDADDR) { | |
370 | cg = itog(fs, ip->i_number); | |
b6407c9d | 371 | return (fs->fs_fpg * cg + fs->fs_frag); |
743f1ef7 | 372 | } |
4f083fd7 SL |
373 | /* |
374 | * Find a cylinder with greater than average number of | |
375 | * unused data blocks. | |
376 | */ | |
16e7863f KM |
377 | if (indx == 0 || bap[indx - 1] == 0) |
378 | startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg; | |
379 | else | |
380 | startcg = dtog(fs, bap[indx - 1]) + 1; | |
381 | startcg %= fs->fs_ncg; | |
4f083fd7 | 382 | avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg; |
16e7863f | 383 | for (cg = startcg; cg < fs->fs_ncg; cg++) |
4f083fd7 SL |
384 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { |
385 | fs->fs_cgrotor = cg; | |
386 | return (fs->fs_fpg * cg + fs->fs_frag); | |
387 | } | |
16e7863f | 388 | for (cg = 0; cg <= startcg; cg++) |
4f083fd7 SL |
389 | if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) { |
390 | fs->fs_cgrotor = cg; | |
391 | return (fs->fs_fpg * cg + fs->fs_frag); | |
392 | } | |
393 | return (NULL); | |
394 | } | |
395 | /* | |
396 | * One or more previous blocks have been laid out. If less | |
397 | * than fs_maxcontig previous blocks are contiguous, the | |
398 | * next block is requested contiguously, otherwise it is | |
399 | * requested rotationally delayed by fs_rotdelay milliseconds. | |
400 | */ | |
401 | nextblk = bap[indx - 1] + fs->fs_frag; | |
402 | if (indx > fs->fs_maxcontig && | |
240a4664 | 403 | bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig) |
4f083fd7 SL |
404 | != nextblk) |
405 | return (nextblk); | |
406 | if (fs->fs_rotdelay != 0) | |
407 | /* | |
408 | * Here we convert ms of delay to frags as: | |
409 | * (frags) = (ms) * (rev/sec) * (sect/rev) / | |
410 | * ((sect/frag) * (ms/sec)) | |
411 | * then round up to the next block. | |
412 | */ | |
413 | nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect / | |
414 | (NSPF(fs) * 1000), fs->fs_frag); | |
415 | return (nextblk); | |
743f1ef7 KM |
416 | } |
417 | ||
502770a3 KM |
418 | /* |
419 | * Implement the cylinder overflow algorithm. | |
420 | * | |
421 | * The policy implemented by this algorithm is: | |
422 | * 1) allocate the block in its requested cylinder group. | |
423 | * 2) quadradically rehash on the cylinder group number. | |
424 | * 3) brute force search for a free block. | |
425 | */ | |
daaf7bee KM |
426 | /*VARARGS5*/ |
427 | u_long | |
f7287e4b KM |
428 | hashalloc(ip, cg, pref, size, allocator) |
429 | struct inode *ip; | |
e3fe2d69 KM |
430 | int cg; |
431 | long pref; | |
432 | int size; /* size for data blocks, mode for inodes */ | |
daaf7bee | 433 | u_long (*allocator)(); |
e3fe2d69 | 434 | { |
f7287e4b | 435 | register struct fs *fs; |
e3fe2d69 KM |
436 | long result; |
437 | int i, icg = cg; | |
438 | ||
f7287e4b | 439 | fs = ip->i_fs; |
e3fe2d69 KM |
440 | /* |
441 | * 1: preferred cylinder group | |
442 | */ | |
f7287e4b | 443 | result = (*allocator)(ip, cg, pref, size); |
e3fe2d69 KM |
444 | if (result) |
445 | return (result); | |
446 | /* | |
447 | * 2: quadratic rehash | |
448 | */ | |
449 | for (i = 1; i < fs->fs_ncg; i *= 2) { | |
450 | cg += i; | |
451 | if (cg >= fs->fs_ncg) | |
452 | cg -= fs->fs_ncg; | |
f7287e4b | 453 | result = (*allocator)(ip, cg, 0, size); |
e3fe2d69 KM |
454 | if (result) |
455 | return (result); | |
456 | } | |
457 | /* | |
458 | * 3: brute force search | |
620b3290 SL |
459 | * Note that we start at i == 2, since 0 was checked initially, |
460 | * and 1 is always checked in the quadratic rehash. | |
e3fe2d69 | 461 | */ |
2136305e | 462 | cg = (icg + 2) % fs->fs_ncg; |
620b3290 | 463 | for (i = 2; i < fs->fs_ncg; i++) { |
f7287e4b | 464 | result = (*allocator)(ip, cg, 0, size); |
e3fe2d69 KM |
465 | if (result) |
466 | return (result); | |
467 | cg++; | |
468 | if (cg == fs->fs_ncg) | |
469 | cg = 0; | |
470 | } | |
ae851115 | 471 | return (NULL); |
e3fe2d69 KM |
472 | } |
473 | ||
502770a3 KM |
474 | /* |
475 | * Determine whether a fragment can be extended. | |
476 | * | |
477 | * Check to see if the necessary fragments are available, and | |
478 | * if they are, allocate them. | |
479 | */ | |
07670f7d | 480 | daddr_t |
f7287e4b KM |
481 | fragextend(ip, cg, bprev, osize, nsize) |
482 | struct inode *ip; | |
07670f7d | 483 | int cg; |
f3c028b7 | 484 | long bprev; |
07670f7d KM |
485 | int osize, nsize; |
486 | { | |
f7287e4b | 487 | register struct fs *fs; |
f3c028b7 KM |
488 | register struct buf *bp; |
489 | register struct cg *cgp; | |
490 | long bno; | |
491 | int frags, bbase; | |
07670f7d KM |
492 | int i; |
493 | ||
f7287e4b | 494 | fs = ip->i_fs; |
a8580723 | 495 | if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize)) |
e5476900 | 496 | return (NULL); |
d995d89d | 497 | frags = numfrags(fs, nsize); |
a8580723 KM |
498 | bbase = fragnum(fs, bprev); |
499 | if (bbase > fragnum(fs, (bprev + frags - 1))) { | |
f3c028b7 | 500 | /* cannot extend across a block boundry */ |
ae851115 | 501 | return (NULL); |
f3c028b7 | 502 | } |
d65bd829 | 503 | bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); |
e5476900 KM |
504 | cgp = bp->b_un.b_cg; |
505 | if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { | |
d995d89d | 506 | brelse(bp); |
ae851115 | 507 | return (NULL); |
d995d89d | 508 | } |
95d0807c | 509 | cgp->cg_time = time.tv_sec; |
6994bf5d | 510 | bno = dtogd(fs, bprev); |
d995d89d | 511 | for (i = numfrags(fs, osize); i < frags; i++) |
aca50d72 KM |
512 | if (isclr(cgp->cg_free, bno + i)) { |
513 | brelse(bp); | |
ae851115 | 514 | return (NULL); |
aca50d72 KM |
515 | } |
516 | /* | |
517 | * the current fragment can be extended | |
518 | * deduct the count on fragment being extended into | |
519 | * increase the count on the remaining fragment (if any) | |
520 | * allocate the extended piece | |
521 | */ | |
522 | for (i = frags; i < fs->fs_frag - bbase; i++) | |
f3c028b7 KM |
523 | if (isclr(cgp->cg_free, bno + i)) |
524 | break; | |
d995d89d | 525 | cgp->cg_frsum[i - numfrags(fs, osize)]--; |
aca50d72 KM |
526 | if (i != frags) |
527 | cgp->cg_frsum[i - frags]++; | |
d995d89d | 528 | for (i = numfrags(fs, osize); i < frags; i++) { |
aca50d72 KM |
529 | clrbit(cgp->cg_free, bno + i); |
530 | cgp->cg_cs.cs_nffree--; | |
531 | fs->fs_cstotal.cs_nffree--; | |
532 | fs->fs_cs(fs, cg).cs_nffree--; | |
f3c028b7 | 533 | } |
aca50d72 KM |
534 | fs->fs_fmod++; |
535 | bdwrite(bp); | |
536 | return (bprev); | |
07670f7d KM |
537 | } |
538 | ||
502770a3 KM |
539 | /* |
540 | * Determine whether a block can be allocated. | |
541 | * | |
542 | * Check to see if a block of the apprpriate size is available, | |
543 | * and if it is, allocate it. | |
544 | */ | |
4f083fd7 | 545 | daddr_t |
f7287e4b KM |
546 | alloccg(ip, cg, bpref, size) |
547 | struct inode *ip; | |
e3fe2d69 KM |
548 | int cg; |
549 | daddr_t bpref; | |
550 | int size; | |
551 | { | |
f7287e4b | 552 | register struct fs *fs; |
f3c028b7 KM |
553 | register struct buf *bp; |
554 | register struct cg *cgp; | |
555 | int bno, frags; | |
556 | int allocsiz; | |
f3c028b7 | 557 | register int i; |
e3fe2d69 | 558 | |
f7287e4b | 559 | fs = ip->i_fs; |
b6407c9d | 560 | if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize) |
ae851115 | 561 | return (NULL); |
d65bd829 | 562 | bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); |
e5476900 | 563 | cgp = bp->b_un.b_cg; |
0f538882 KM |
564 | if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || |
565 | (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) { | |
d995d89d | 566 | brelse(bp); |
ae851115 | 567 | return (NULL); |
d995d89d | 568 | } |
95d0807c | 569 | cgp->cg_time = time.tv_sec; |
b6407c9d | 570 | if (size == fs->fs_bsize) { |
daaf7bee | 571 | bno = alloccgblk(fs, cgp, bpref); |
f3c028b7 KM |
572 | bdwrite(bp); |
573 | return (bno); | |
574 | } | |
575 | /* | |
576 | * check to see if any fragments are already available | |
577 | * allocsiz is the size which will be allocated, hacking | |
578 | * it down to a smaller size if necessary | |
579 | */ | |
d995d89d | 580 | frags = numfrags(fs, size); |
b6407c9d | 581 | for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++) |
f3c028b7 KM |
582 | if (cgp->cg_frsum[allocsiz] != 0) |
583 | break; | |
b6407c9d | 584 | if (allocsiz == fs->fs_frag) { |
f3c028b7 KM |
585 | /* |
586 | * no fragments were available, so a block will be | |
587 | * allocated, and hacked up | |
588 | */ | |
0947395d | 589 | if (cgp->cg_cs.cs_nbfree == 0) { |
f3c028b7 | 590 | brelse(bp); |
ae851115 | 591 | return (NULL); |
f3c028b7 | 592 | } |
daaf7bee | 593 | bno = alloccgblk(fs, cgp, bpref); |
6994bf5d | 594 | bpref = dtogd(fs, bno); |
b6407c9d | 595 | for (i = frags; i < fs->fs_frag; i++) |
f3c028b7 | 596 | setbit(cgp->cg_free, bpref + i); |
b6407c9d | 597 | i = fs->fs_frag - frags; |
0947395d KM |
598 | cgp->cg_cs.cs_nffree += i; |
599 | fs->fs_cstotal.cs_nffree += i; | |
b6407c9d | 600 | fs->fs_cs(fs, cg).cs_nffree += i; |
961945a8 | 601 | fs->fs_fmod++; |
f3c028b7 KM |
602 | cgp->cg_frsum[i]++; |
603 | bdwrite(bp); | |
604 | return (bno); | |
605 | } | |
743f1ef7 | 606 | bno = mapsearch(fs, cgp, bpref, allocsiz); |
0f538882 KM |
607 | if (bno < 0) { |
608 | brelse(bp); | |
ae851115 | 609 | return (NULL); |
0f538882 | 610 | } |
f3c028b7 KM |
611 | for (i = 0; i < frags; i++) |
612 | clrbit(cgp->cg_free, bno + i); | |
0947395d KM |
613 | cgp->cg_cs.cs_nffree -= frags; |
614 | fs->fs_cstotal.cs_nffree -= frags; | |
b6407c9d | 615 | fs->fs_cs(fs, cg).cs_nffree -= frags; |
961945a8 | 616 | fs->fs_fmod++; |
f3c028b7 KM |
617 | cgp->cg_frsum[allocsiz]--; |
618 | if (frags != allocsiz) | |
619 | cgp->cg_frsum[allocsiz - frags]++; | |
620 | bdwrite(bp); | |
621 | return (cg * fs->fs_fpg + bno); | |
622 | } | |
623 | ||
502770a3 KM |
624 | /* |
625 | * Allocate a block in a cylinder group. | |
626 | * | |
627 | * This algorithm implements the following policy: | |
628 | * 1) allocate the requested block. | |
629 | * 2) allocate a rotationally optimal block in the same cylinder. | |
630 | * 3) allocate the next available block on the block rotor for the | |
631 | * specified cylinder group. | |
632 | * Note that this routine only allocates fs_bsize blocks; these | |
633 | * blocks may be fragmented by the routine that allocates them. | |
634 | */ | |
f3c028b7 | 635 | daddr_t |
daaf7bee | 636 | alloccgblk(fs, cgp, bpref) |
f7287e4b | 637 | register struct fs *fs; |
f3c028b7 KM |
638 | register struct cg *cgp; |
639 | daddr_t bpref; | |
640 | { | |
743f1ef7 | 641 | daddr_t bno; |
ae851115 | 642 | int cylno, pos, delta; |
743f1ef7 | 643 | short *cylbp; |
aca50d72 | 644 | register int i; |
f3c028b7 | 645 | |
743f1ef7 KM |
646 | if (bpref == 0) { |
647 | bpref = cgp->cg_rotor; | |
aca50d72 KM |
648 | goto norot; |
649 | } | |
a8580723 | 650 | bpref = blknum(fs, bpref); |
6994bf5d | 651 | bpref = dtogd(fs, bpref); |
aca50d72 KM |
652 | /* |
653 | * if the requested block is available, use it | |
654 | */ | |
240a4664 | 655 | if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) { |
aca50d72 KM |
656 | bno = bpref; |
657 | goto gotit; | |
658 | } | |
aca50d72 KM |
659 | /* |
660 | * check for a block available on the same cylinder | |
aca50d72 KM |
661 | */ |
662 | cylno = cbtocylno(fs, bpref); | |
502770a3 KM |
663 | if (cgp->cg_btot[cylno] == 0) |
664 | goto norot; | |
665 | if (fs->fs_cpc == 0) { | |
666 | /* | |
667 | * block layout info is not available, so just have | |
668 | * to take any block in this cylinder. | |
669 | */ | |
670 | bpref = howmany(fs->fs_spc * cylno, NSPF(fs)); | |
671 | goto norot; | |
672 | } | |
aca50d72 KM |
673 | /* |
674 | * check the summary information to see if a block is | |
675 | * available in the requested cylinder starting at the | |
4f083fd7 | 676 | * requested rotational position and proceeding around. |
aca50d72 | 677 | */ |
4f083fd7 SL |
678 | cylbp = cgp->cg_b[cylno]; |
679 | pos = cbtorpos(fs, bpref); | |
aca50d72 KM |
680 | for (i = pos; i < NRPOS; i++) |
681 | if (cylbp[i] > 0) | |
682 | break; | |
683 | if (i == NRPOS) | |
684 | for (i = 0; i < pos; i++) | |
743f1ef7 KM |
685 | if (cylbp[i] > 0) |
686 | break; | |
aca50d72 KM |
687 | if (cylbp[i] > 0) { |
688 | /* | |
689 | * found a rotational position, now find the actual | |
690 | * block. A panic if none is actually there. | |
691 | */ | |
692 | pos = cylno % fs->fs_cpc; | |
693 | bno = (cylno - pos) * fs->fs_spc / NSPB(fs); | |
ffd90e52 KM |
694 | if (fs->fs_postbl[pos][i] == -1) { |
695 | printf("pos = %d, i = %d, fs = %s\n", | |
696 | pos, i, fs->fs_fsmnt); | |
aca50d72 | 697 | panic("alloccgblk: cyl groups corrupted"); |
ffd90e52 | 698 | } |
ae851115 | 699 | for (i = fs->fs_postbl[pos][i];; ) { |
aca50d72 | 700 | if (isblock(fs, cgp->cg_free, bno + i)) { |
240a4664 | 701 | bno = blkstofrags(fs, (bno + i)); |
aca50d72 | 702 | goto gotit; |
743f1ef7 | 703 | } |
ae851115 KM |
704 | delta = fs->fs_rotbl[i]; |
705 | if (delta <= 0 || delta > MAXBPC - i) | |
aca50d72 | 706 | break; |
ae851115 | 707 | i += delta; |
743f1ef7 | 708 | } |
ffd90e52 | 709 | printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt); |
aca50d72 | 710 | panic("alloccgblk: can't find blk in cyl"); |
e3fe2d69 | 711 | } |
aca50d72 KM |
712 | norot: |
713 | /* | |
714 | * no blocks in the requested cylinder, so take next | |
715 | * available one in this cylinder group. | |
716 | */ | |
b32450f4 | 717 | bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag); |
6459ebe0 | 718 | if (bno < 0) |
ae851115 | 719 | return (NULL); |
743f1ef7 | 720 | cgp->cg_rotor = bno; |
e3fe2d69 | 721 | gotit: |
240a4664 | 722 | clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno)); |
0947395d KM |
723 | cgp->cg_cs.cs_nbfree--; |
724 | fs->fs_cstotal.cs_nbfree--; | |
b6407c9d | 725 | fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--; |
502770a3 KM |
726 | cylno = cbtocylno(fs, bno); |
727 | cgp->cg_b[cylno][cbtorpos(fs, bno)]--; | |
728 | cgp->cg_btot[cylno]--; | |
e3fe2d69 | 729 | fs->fs_fmod++; |
743f1ef7 | 730 | return (cgp->cg_cgx * fs->fs_fpg + bno); |
e3fe2d69 KM |
731 | } |
732 | ||
502770a3 KM |
733 | /* |
734 | * Determine whether an inode can be allocated. | |
735 | * | |
736 | * Check to see if an inode is available, and if it is, | |
737 | * allocate it using the following policy: | |
738 | * 1) allocate the requested inode. | |
739 | * 2) allocate the next available inode after the requested | |
740 | * inode in the specified cylinder group. | |
741 | */ | |
4f083fd7 | 742 | ino_t |
f7287e4b KM |
743 | ialloccg(ip, cg, ipref, mode) |
744 | struct inode *ip; | |
e3fe2d69 KM |
745 | int cg; |
746 | daddr_t ipref; | |
747 | int mode; | |
748 | { | |
f7287e4b | 749 | register struct fs *fs; |
f3c028b7 | 750 | register struct cg *cgp; |
4e0c7b8a KM |
751 | struct buf *bp; |
752 | int start, len, loc, map, i; | |
e3fe2d69 | 753 | |
f7287e4b | 754 | fs = ip->i_fs; |
b6407c9d | 755 | if (fs->fs_cs(fs, cg).cs_nifree == 0) |
ae851115 | 756 | return (NULL); |
d65bd829 | 757 | bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); |
e5476900 | 758 | cgp = bp->b_un.b_cg; |
0f538882 KM |
759 | if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC || |
760 | cgp->cg_cs.cs_nifree == 0) { | |
d995d89d | 761 | brelse(bp); |
ae851115 | 762 | return (NULL); |
d995d89d | 763 | } |
95d0807c | 764 | cgp->cg_time = time.tv_sec; |
e3fe2d69 KM |
765 | if (ipref) { |
766 | ipref %= fs->fs_ipg; | |
767 | if (isclr(cgp->cg_iused, ipref)) | |
768 | goto gotit; | |
4e0c7b8a KM |
769 | } |
770 | start = cgp->cg_irotor / NBBY; | |
771 | len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY); | |
772 | loc = skpc(0xff, len, &cgp->cg_iused[start]); | |
773 | if (loc == 0) { | |
e5889092 KM |
774 | len = start + 1; |
775 | start = 0; | |
776 | loc = skpc(0xff, len, &cgp->cg_iused[0]); | |
777 | if (loc == 0) { | |
778 | printf("cg = %s, irotor = %d, fs = %s\n", | |
779 | cg, cgp->cg_irotor, fs->fs_fsmnt); | |
780 | panic("ialloccg: map corrupted"); | |
781 | /* NOTREACHED */ | |
782 | } | |
4e0c7b8a KM |
783 | } |
784 | i = start + len - loc; | |
785 | map = cgp->cg_iused[i]; | |
786 | ipref = i * NBBY; | |
787 | for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) { | |
788 | if ((map & i) == 0) { | |
e3fe2d69 KM |
789 | cgp->cg_irotor = ipref; |
790 | goto gotit; | |
791 | } | |
792 | } | |
4e0c7b8a KM |
793 | printf("fs = %s\n", fs->fs_fsmnt); |
794 | panic("ialloccg: block not in map"); | |
795 | /* NOTREACHED */ | |
e3fe2d69 KM |
796 | gotit: |
797 | setbit(cgp->cg_iused, ipref); | |
0947395d KM |
798 | cgp->cg_cs.cs_nifree--; |
799 | fs->fs_cstotal.cs_nifree--; | |
b6407c9d | 800 | fs->fs_cs(fs, cg).cs_nifree--; |
e3fe2d69 KM |
801 | fs->fs_fmod++; |
802 | if ((mode & IFMT) == IFDIR) { | |
0947395d KM |
803 | cgp->cg_cs.cs_ndir++; |
804 | fs->fs_cstotal.cs_ndir++; | |
b6407c9d | 805 | fs->fs_cs(fs, cg).cs_ndir++; |
e3fe2d69 KM |
806 | } |
807 | bdwrite(bp); | |
808 | return (cg * fs->fs_ipg + ipref); | |
809 | } | |
810 | ||
502770a3 KM |
811 | /* |
812 | * Free a block or fragment. | |
813 | * | |
814 | * The specified block or fragment is placed back in the | |
815 | * free map. If a fragment is deallocated, a possible | |
816 | * block reassembly is checked. | |
817 | */ | |
4f083fd7 | 818 | free(ip, bno, size) |
f7287e4b | 819 | register struct inode *ip; |
e3fe2d69 | 820 | daddr_t bno; |
daaf7bee | 821 | off_t size; |
e3fe2d69 KM |
822 | { |
823 | register struct fs *fs; | |
824 | register struct cg *cgp; | |
825 | register struct buf *bp; | |
f3c028b7 KM |
826 | int cg, blk, frags, bbase; |
827 | register int i; | |
e3fe2d69 | 828 | |
f7287e4b | 829 | fs = ip->i_fs; |
ffd90e52 KM |
830 | if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) { |
831 | printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n", | |
832 | ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt); | |
b6407c9d | 833 | panic("free: bad size"); |
ffd90e52 | 834 | } |
6994bf5d | 835 | cg = dtog(fs, bno); |
6459ebe0 KM |
836 | if (badblock(fs, bno)) { |
837 | printf("bad block %d, ino %d\n", bno, ip->i_number); | |
e3fe2d69 | 838 | return; |
6459ebe0 | 839 | } |
d65bd829 | 840 | bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); |
e5476900 KM |
841 | cgp = bp->b_un.b_cg; |
842 | if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { | |
d995d89d | 843 | brelse(bp); |
e3fe2d69 | 844 | return; |
d995d89d | 845 | } |
95d0807c | 846 | cgp->cg_time = time.tv_sec; |
6994bf5d | 847 | bno = dtogd(fs, bno); |
b6407c9d | 848 | if (size == fs->fs_bsize) { |
240a4664 | 849 | if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) { |
ffd90e52 KM |
850 | printf("dev = 0x%x, block = %d, fs = %s\n", |
851 | ip->i_dev, bno, fs->fs_fsmnt); | |
07670f7d | 852 | panic("free: freeing free block"); |
6459ebe0 | 853 | } |
240a4664 | 854 | setblock(fs, cgp->cg_free, fragstoblks(fs, bno)); |
0947395d KM |
855 | cgp->cg_cs.cs_nbfree++; |
856 | fs->fs_cstotal.cs_nbfree++; | |
b6407c9d | 857 | fs->fs_cs(fs, cg).cs_nbfree++; |
502770a3 KM |
858 | i = cbtocylno(fs, bno); |
859 | cgp->cg_b[i][cbtorpos(fs, bno)]++; | |
860 | cgp->cg_btot[i]++; | |
07670f7d | 861 | } else { |
a8580723 | 862 | bbase = bno - fragnum(fs, bno); |
f3c028b7 KM |
863 | /* |
864 | * decrement the counts associated with the old frags | |
865 | */ | |
ae851115 | 866 | blk = blkmap(fs, cgp->cg_free, bbase); |
b6407c9d | 867 | fragacct(fs, blk, cgp->cg_frsum, -1); |
f3c028b7 KM |
868 | /* |
869 | * deallocate the fragment | |
870 | */ | |
d995d89d | 871 | frags = numfrags(fs, size); |
f3c028b7 | 872 | for (i = 0; i < frags; i++) { |
ffd90e52 KM |
873 | if (isset(cgp->cg_free, bno + i)) { |
874 | printf("dev = 0x%x, block = %d, fs = %s\n", | |
875 | ip->i_dev, bno + i, fs->fs_fsmnt); | |
07670f7d | 876 | panic("free: freeing free frag"); |
ffd90e52 | 877 | } |
07670f7d | 878 | setbit(cgp->cg_free, bno + i); |
07670f7d | 879 | } |
ae851115 KM |
880 | cgp->cg_cs.cs_nffree += i; |
881 | fs->fs_cstotal.cs_nffree += i; | |
882 | fs->fs_cs(fs, cg).cs_nffree += i; | |
f3c028b7 KM |
883 | /* |
884 | * add back in counts associated with the new frags | |
885 | */ | |
ae851115 | 886 | blk = blkmap(fs, cgp->cg_free, bbase); |
b6407c9d | 887 | fragacct(fs, blk, cgp->cg_frsum, 1); |
f3c028b7 KM |
888 | /* |
889 | * if a complete block has been reassembled, account for it | |
890 | */ | |
240a4664 | 891 | if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) { |
b6407c9d KM |
892 | cgp->cg_cs.cs_nffree -= fs->fs_frag; |
893 | fs->fs_cstotal.cs_nffree -= fs->fs_frag; | |
894 | fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag; | |
0947395d KM |
895 | cgp->cg_cs.cs_nbfree++; |
896 | fs->fs_cstotal.cs_nbfree++; | |
b6407c9d | 897 | fs->fs_cs(fs, cg).cs_nbfree++; |
502770a3 KM |
898 | i = cbtocylno(fs, bbase); |
899 | cgp->cg_b[i][cbtorpos(fs, bbase)]++; | |
900 | cgp->cg_btot[i]++; | |
07670f7d KM |
901 | } |
902 | } | |
e3fe2d69 | 903 | fs->fs_fmod++; |
e3fe2d69 KM |
904 | bdwrite(bp); |
905 | } | |
906 | ||
502770a3 KM |
907 | /* |
908 | * Free an inode. | |
909 | * | |
910 | * The specified inode is placed back in the free map. | |
911 | */ | |
f7287e4b KM |
912 | ifree(ip, ino, mode) |
913 | struct inode *ip; | |
e3fe2d69 KM |
914 | ino_t ino; |
915 | int mode; | |
916 | { | |
917 | register struct fs *fs; | |
918 | register struct cg *cgp; | |
919 | register struct buf *bp; | |
e3fe2d69 KM |
920 | int cg; |
921 | ||
f7287e4b | 922 | fs = ip->i_fs; |
ffd90e52 KM |
923 | if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) { |
924 | printf("dev = 0x%x, ino = %d, fs = %s\n", | |
925 | ip->i_dev, ino, fs->fs_fsmnt); | |
e3fe2d69 | 926 | panic("ifree: range"); |
ffd90e52 | 927 | } |
6994bf5d | 928 | cg = itog(fs, ino); |
d65bd829 | 929 | bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize); |
e5476900 KM |
930 | cgp = bp->b_un.b_cg; |
931 | if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) { | |
d995d89d | 932 | brelse(bp); |
e3fe2d69 | 933 | return; |
d995d89d | 934 | } |
95d0807c | 935 | cgp->cg_time = time.tv_sec; |
e3fe2d69 | 936 | ino %= fs->fs_ipg; |
ffd90e52 KM |
937 | if (isclr(cgp->cg_iused, ino)) { |
938 | printf("dev = 0x%x, ino = %d, fs = %s\n", | |
939 | ip->i_dev, ino, fs->fs_fsmnt); | |
e3fe2d69 | 940 | panic("ifree: freeing free inode"); |
ffd90e52 | 941 | } |
e3fe2d69 | 942 | clrbit(cgp->cg_iused, ino); |
4e0c7b8a KM |
943 | if (ino < cgp->cg_irotor) |
944 | cgp->cg_irotor = ino; | |
0947395d KM |
945 | cgp->cg_cs.cs_nifree++; |
946 | fs->fs_cstotal.cs_nifree++; | |
b6407c9d | 947 | fs->fs_cs(fs, cg).cs_nifree++; |
e3fe2d69 | 948 | if ((mode & IFMT) == IFDIR) { |
0947395d KM |
949 | cgp->cg_cs.cs_ndir--; |
950 | fs->fs_cstotal.cs_ndir--; | |
b6407c9d | 951 | fs->fs_cs(fs, cg).cs_ndir--; |
e3fe2d69 KM |
952 | } |
953 | fs->fs_fmod++; | |
954 | bdwrite(bp); | |
955 | } | |
956 | ||
743f1ef7 | 957 | /* |
502770a3 KM |
958 | * Find a block of the specified size in the specified cylinder group. |
959 | * | |
743f1ef7 KM |
960 | * It is a panic if a request is made to find a block if none are |
961 | * available. | |
962 | */ | |
963 | daddr_t | |
964 | mapsearch(fs, cgp, bpref, allocsiz) | |
965 | register struct fs *fs; | |
966 | register struct cg *cgp; | |
967 | daddr_t bpref; | |
968 | int allocsiz; | |
969 | { | |
970 | daddr_t bno; | |
971 | int start, len, loc, i; | |
972 | int blk, field, subfield, pos; | |
973 | ||
974 | /* | |
975 | * find the fragment by searching through the free block | |
976 | * map for an appropriate bit pattern | |
977 | */ | |
978 | if (bpref) | |
6994bf5d | 979 | start = dtogd(fs, bpref) / NBBY; |
743f1ef7 KM |
980 | else |
981 | start = cgp->cg_frotor / NBBY; | |
942bd18b | 982 | len = howmany(fs->fs_fpg, NBBY) - start; |
88a7a62a SL |
983 | loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start], |
984 | (caddr_t)fragtbl[fs->fs_frag], | |
985 | (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); | |
743f1ef7 | 986 | if (loc == 0) { |
e5476900 KM |
987 | len = start + 1; |
988 | start = 0; | |
e5889092 | 989 | loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[0], |
88a7a62a SL |
990 | (caddr_t)fragtbl[fs->fs_frag], |
991 | (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY)))); | |
4e0c7b8a KM |
992 | if (loc == 0) { |
993 | printf("start = %d, len = %d, fs = %s\n", | |
994 | start, len, fs->fs_fsmnt); | |
995 | panic("alloccg: map corrupted"); | |
e5889092 | 996 | /* NOTREACHED */ |
4e0c7b8a | 997 | } |
743f1ef7 KM |
998 | } |
999 | bno = (start + len - loc) * NBBY; | |
1000 | cgp->cg_frotor = bno; | |
1001 | /* | |
1002 | * found the byte in the map | |
1003 | * sift through the bits to find the selected frag | |
1004 | */ | |
ae851115 KM |
1005 | for (i = bno + NBBY; bno < i; bno += fs->fs_frag) { |
1006 | blk = blkmap(fs, cgp->cg_free, bno); | |
743f1ef7 KM |
1007 | blk <<= 1; |
1008 | field = around[allocsiz]; | |
1009 | subfield = inside[allocsiz]; | |
b6407c9d | 1010 | for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) { |
ae851115 KM |
1011 | if ((blk & field) == subfield) |
1012 | return (bno + pos); | |
743f1ef7 KM |
1013 | field <<= 1; |
1014 | subfield <<= 1; | |
1015 | } | |
1016 | } | |
ffd90e52 | 1017 | printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt); |
743f1ef7 | 1018 | panic("alloccg: block not in map"); |
e5476900 | 1019 | return (-1); |
743f1ef7 KM |
1020 | } |
1021 | ||
e3fe2d69 | 1022 | /* |
502770a3 KM |
1023 | * Fserr prints the name of a file system with an error diagnostic. |
1024 | * | |
1025 | * The form of the error message is: | |
e3fe2d69 KM |
1026 | * fs: error message |
1027 | */ | |
1028 | fserr(fs, cp) | |
1029 | struct fs *fs; | |
1030 | char *cp; | |
1031 | { | |
1032 | ||
283ffc90 | 1033 | log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp); |
e3fe2d69 | 1034 | } |